Skip to content

Instantly share code, notes, and snippets.

@leosabbir
Created June 17, 2019 15:28
Show Gist options
  • Save leosabbir/704c7f01c8300954054fa5a2c77f26af to your computer and use it in GitHub Desktop.
Save leosabbir/704c7f01c8300954054fa5a2c77f26af to your computer and use it in GitHub Desktop.
Daily Coding Problem #332
import java.io.*;
import java.util.Scanner;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
long X = sc.nextLong();
long S = sc.nextLong();
long A = (S-X);
if (A%2 == 1) {
System.out.println("none");
return;
}
A = A >> 1;
int answers = 1;
for(int i = 1; i <= 64; i++) {
int xi =(int) (X & 1); // obtain LSB of XOR
int ai = (int) (A & 1); // obtain LSB of AND
// XOR bit is 1, AND bit is 0 which means two number bits can be either 0 and 1 OR 1 and 0
if (xi == 1 && ai == 0) answers *= 2;
// XOR bit is 1, AND bit is 1 , which means there can't be such numbers
else if (xi == 1 && ai == 1) {
System.out.println("none");
return;
}
// XOR bit is 0, AND bit is 0, there can be only one choice: 0 and 0,
// XOR bit is 0, AND bit is 1, there can be only one choice: 1 and 1
X = X >> 1;
A = A >> 1;
}
System.out.println(answers + " Pairs");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment