Input: M =9, N =5Output: 2Explanation:
The pairs that satisfy both conditions are:-(2,7):2+7=9,2 XOR 7=5✓-(7,2):7+2=9,7 XOR 2=5✓Since we need positive integers, both pairs are valid.
Input: M =6, N =2Output: 2Explanation:
The pairs that satisfy both conditions are:-(2,4):2+4=6,2 XOR 4=2✓-(4,2):4+2=6,4 XOR 2=2✓Both pairs contain positive integers.
Input: M =3, N =5Output: 0Explanation:
No positive integer pairs can satisfy both conditions.For a + b =3 and a XOR b =5, we would need negative values.
We can use the mathematical relationship between addition and XOR. For any two numbers a and b: a + b = (a XOR b) + 2 * (a AND b). Given that a + b = M and a XOR b = N, we can derive a AND b = (M - N) / 2. If this value is non-negative and integer, we can check if valid pairs exist using bit manipulation properties.
classSolution {
public:int countPairs(int M, int N) {
// Check if (M - N) is even and non-negative
if ((M - N) %2!=0|| M - N <0) {
return0;
}
int andVal = (M - N) /2;
int xorVal = N;
// Check if andVal and xorVal have overlapping bits
if ((andVal & xorVal) !=0) {
return0;
}
// Count set bits in xorVal
int setBits = __builtin_popcount(xorVal);
int totalPairs =1<< setBits; // 2^setBits
// Check each possible pair and count valid ones
int validPairs =0;
for (int i =0; i < totalPairs; i++) {
int a = andVal;
int b = andVal;
// Distribute XOR bits
for (int bit =0; bit <32; bit++) {
if ((xorVal >> bit) &1) {
if ((i >> __builtin_popcount(xorVal & ((1<< bit) -1))) &1) {
a |= (1<< bit);
} else {
b |= (1<< bit);
}
}
}
if (a >0&& b >0) {
validPairs++;
}
}
return validPairs;
}
};
classSolution {
publicintcountPairs(int M, int N) {
// Check if (M - N) is even and non-negativeif ((M - N) % 2 != 0 || M - N < 0) {
return 0;
}
int andVal = (M - N) / 2;
int xorVal = N;
// Check if andVal and xorVal have overlapping bitsif ((andVal & xorVal) != 0) {
return 0;
}
// Count set bits in xorValint setBits = Integer.bitCount(xorVal);
int totalPairs = 1 << setBits; // 2^setBits// Check each possible pair and count valid onesint validPairs = 0;
for (int i = 0; i < totalPairs; i++) {
int a = andVal;
int b = andVal;
// Distribute XOR bitsint bitIdx = 0;
for (int bit = 0; bit < 32; bit++) {
if ((xorVal >> bit & 1) == 1) {
if ((i >> bitIdx & 1) == 1) {
a |= (1 << bit);
} else {
b |= (1 << bit);
}
bitIdx++;
}
}
if (a > 0 && b > 0) {
validPairs++;
}
}
return validPairs;
}
}
classSolution:
defcount_pairs(self, M: int, N: int) -> int:
# Check if (M - N) is even and non-negativeif (M - N) %2!=0or M - N <0:
return0 and_val = (M - N) //2 xor_val = N
# Check if and_val and xor_val have overlapping bitsif (and_val & xor_val) !=0:
return0# Count set bits in xor_val set_bits = bin(xor_val).count('1')
total_pairs =1<< set_bits # 2^set_bits# Check each possible pair and count valid ones valid_pairs =0for i in range(total_pairs):
a = and_val
b = and_val
# Distribute XOR bits bit_idx =0for bit in range(32):
if (xor_val >> bit) &1:
if (i >> bit_idx) &1:
a |= (1<< bit)
else:
b |= (1<< bit)
bit_idx +=1if a >0and b >0:
valid_pairs +=1return valid_pairs
We can directly calculate the result without generating all pairs. After verifying that valid pairs can exist using the mathematical constraints, we check the boundary conditions. If N = 0, there’s exactly one pair (M/2, M/2) if M is even. Otherwise, we have 2^k total combinations, but we need to subtract cases where either number becomes zero or negative.
classSolution {
public:int countPairsDirect(int M, int N) {
// Basic validation
if ((M - N) %2!=0|| M - N <0) {
return0;
}
int andVal = (M - N) /2;
// Check bit overlap
if ((andVal & N) !=0) {
return0;
}
// Special case: N = 0
if (N ==0) {
return (M >0&& M %2==0) ?1:0;
}
// Calculate minimum possible values
int minA = andVal; // When all XOR bits go to b
int minB = andVal; // When all XOR bits go to a
// Both must be positive
if (minA <=0|| minB <=0) {
return0;
}
// Count set bits in N
int setBits = __builtin_popcount(N);
return1<< setBits; // 2^setBits
}
};
funccountPairsDirect(M, Nint) int {
// Basic validationif (M-N)%2!=0||M-N < 0 {
return0 }
andVal:= (M-N) /2// Check bit overlapif (andVal&N) !=0 {
return0 }
// Special case: N = 0ifN==0 {
ifM > 0&&M%2==0 {
return1 }
return0 }
// Calculate minimum possible valuesminA:=andVal// When all XOR bits go to bminB:=andVal// When all XOR bits go to a// Both must be positiveifminA<=0||minB<=0 {
return0 }
// Count set bits in NsetBits:=popcount(N)
return1<<setBits// 2^setBits}
classSolution:
defcount_pairs_direct(self, M: int, N: int) -> int:
# Basic validationif (M - N) %2!=0or M - N <0:
return0 and_val = (M - N) //2# Check bit overlapif (and_val & N) !=0:
return0# Special case: N = 0if N ==0:
return1if M >0and M %2==0else0# Calculate minimum possible values min_a = and_val # When all XOR bits go to b min_b = and_val # When all XOR bits go to a# Both must be positiveif min_a <=0or min_b <=0:
return0# Count set bits in N set_bits = bin(N).count('1')
return1<< set_bits # 2^set_bits
A straightforward approach is to iterate through all possible values of a from 1 to M-1, calculate b = M - a, and check if both conditions are satisfied. This method is simple to understand and implement, though not optimal for large values.
classSolution {
public:int countPairsBrute(int M, int N) {
if (M <=1) return0;
int count =0;
for (int a =1; a < M; a++) {
int b = M - a;
if (b >0&& (a ^ b) == N) {
count++;
}
}
return count;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funccountPairsBrute(M, Nint) int {
ifM<=1 {
return0 }
count:=0fora:=1; a < M; a++ {
b:=M-aifb > 0&& (a^b) ==N {
count++ }
}
returncount}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintcountPairsBrute(int M, int N) {
if (M <= 1) return 0;
int count = 0;
for (int a = 1; a < M; a++) {
int b = M - a;
if (b > 0 && (a ^ b) == N) {
count++;
}
}
return count;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defcount_pairs_brute(self, M: int, N: int) -> int:
if M <=1:
return0 count =0for a in range(1, M):
b = M - a
if b >0and (a ^ b) == N:
count +=1return count