AND Plus OR Codechef Solution

Hello Programmers In this post, you will know how to solve the AND Plus OR Codechef Solution. The Problem Code: ANDOR

AND Plus OR Codechef Solution
AND Plus OR Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.


Given an integer x, find two non-negative integers a and b such that (a∧b)+(a∨b)=x(a∧b)+(a∨b)=x, where ∧ is the bitwise AND operation and ∨ is the bitwise OR operation.


  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first and only line of each test case contains a single integer x.


If there is no valid pair (a,b), print a single line containing the integer −1. Otherwise, print a single line containing two space-separated integers aa and bb.

If there are multiple solutions, you may print any one of them.


  • 1≤T≤1051≤T≤105
  • 1≤x≤10181≤x≤1018


Subtask #1 (30 points):

  • 1≤T≤2001≤T≤200
  • 1≤x≤2001≤x≤200

Subtask #2 (70 points): original constraints

Sample Input 1 


Sample Output 1 

0 1
5 3

AND Plus OR CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(;
    int T = sc.nextInt();
    for (int tc = 0; tc < T; ++tc) {
      long x = sc.nextLong();
  static String solve(long x) {
    return String.format("0 %d", x);

Disclaimer: The above Problem (AND Plus OR) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Subtraction Game 1 Codechef Solution

Leave a Reply

Your email address will not be published. Required fields are marked *