Given a positive integer n, count the number of integers i such that: 0 ≤ i ≤ n and n + i = n ^ i, where ^ denotes the bitwise XOR operation.
Examples :
Input: n = 7
Output: 1
Explanation: The condition holds only for i = 0.
7 + 0 = 7
7 ^ 0 = 7
Therefore, the answer is 1.
Input: n = 12
Output: 4
Explanation: The condition holds for: i = 0, 1, 2, 3
12 + 0 = 12 ^ 0 = 12
12 + 1 = 12 ^ 1 = 13
12 + 2 = 12 ^ 2 = 14
12 + 3 = 12 ^ 3 = 15
Therefore, the answer is 4.
Table of Content
[Naive Approach] Check Every Value - O(n) Time and O(1) Space
This is the simple simulation, we can check each & every value to get the output.
#include <iostream>
using namespace std;
int countValues(int n) {
int count = 0;
for (int i = 0; i <= n; i++) {
if ((n + i) == (n ^ i)) {
count++;
}
}
return count;
}
int main() {
cout << countValues(7) << endl;
cout << countValues(12) << endl;
return 0;
}
#include <stdio.h>
int main() {
// code
return 0;
}
class GFG {
static int countValues(int n) {
int count = 0;
for (int i = 0; i <= n; i++) {
if ((n + i) == (n ^ i)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println(countValues(7));
System.out.println(countValues(12));
}
}
def countValues(n):
count = 0
for i in range(n + 1):
if (n + i) == (n ^ i):
count += 1
return count
if __name__ == "__main__":
print(countValues(7))
print(countValues(12))
using System;
class GFG {
static int countValues(int n) {
int count = 0;
for (int i = 0; i <= n; i++) {
if ((n + i) == (n ^ i)) {
count++;
}
}
return count;
}
static void Main() {
Console.WriteLine(countValues(7));
Console.WriteLine(countValues(12));
}
}
function countValues(n) {
let count = 0;
for (let i = 0; i <= n; i++) {
if ((n + i) === (n ^ i)) {
count++;
}
}
return count;
}
// Driver Code
console.log(countValues(7));
console.log(countValues(12));
Output
1 4
[Expected Approach] Count Unset Bits - O(log n) Time and O(1) Space
If n + i = n ^ i, then there must be no carry while adding n and i. So, i can have set bits only at positions where n has unset bits.
If the binary representation of n has c unset bits, then total valid values are 2c.
We know that (n+i) = (n^i) + 2*(n&i). So n + i = n ^ i implies n & i = 0. Hence our problem reduces to finding values of i such that n & i = 0. How to find count of such pairs? We can use the count of unset-bits in the binary representation of n. For n & i to be zero, i must unset all set-bits of n. If the kth bit is set at a particular in n, kth bit in i must be 0 always, else kth bit of i can be 0 or 1
Hence, total such combinations are 2^(count of unset bits in n)
For example, consider n = 12 (Binary representation : 1 1 0 0).
All possible values of i that can unset all bits of n are 0 0 0/1 0/1 where 0/1 implies either 0 or 1. Number of such values of i are 2^2 = 4.
#include <iostream>
using namespace std;
int countValues(int n) {
int unsetBits = 0;
while (n > 0) {
if ((n & 1) == 0) {
unsetBits++;
}
n >>= 1;
}
return 1 << unsetBits;
}
int main() {
cout << countValues(7) << endl;
cout << countValues(12) << endl;
return 0;
}
#include <stdio.h>
int countValues(int n) {
int unsetBits = 0;
while (n > 0) {
if ((n & 1) == 0) {
unsetBits++;
}
n >>= 1;
}
return 1 << unsetBits;
}
int main() {
printf("%d\n", countValues(7));
printf("%d\n", countValues(12));
return 0;
}
class GFG {
static int countValues(int n) {
int unsetBits = 0;
while (n > 0) {
if ((n & 1) == 0) {
unsetBits++;
}
n >>= 1;
}
return 1 << unsetBits;
}
public static void main(String[] args) {
System.out.println(countValues(7));
System.out.println(countValues(12));
}
}
def countValues(n):
unsetBits = 0
while n > 0:
if (n & 1) == 0:
unsetBits += 1
n >>= 1
return 1 << unsetBits
if __name__ == "__main__":
print(countValues(7))
print(countValues(12))
using System;
class GFG {
static int countValues(int n) {
int unsetBits = 0;
while (n > 0) {
if ((n & 1) == 0) {
unsetBits++;
}
n >>= 1;
}
return 1 << unsetBits;
}
static void Main() {
Console.WriteLine(countValues(7));
Console.WriteLine(countValues(12));
}
}
function countValues(n) {
let unsetBits = 0;
while (n > 0) {
if ((n & 1) === 0) {
unsetBits++;
}
n >>= 1;
}
return 1 << unsetBits;
}
// Driver Code
console.log(countValues(7));
console.log(countValues(12));
Output
1 4