Number of values of b such that a = b + (a^b)

Given an integer a. Find the number of solutions of b which satisfies the equation:

a = b + (a^b)

Examples:

Input: a = 4 
Output: 2
The only values of b are 0 and 4 itself.

Input: a = 3 
Output: 4 

A naive solution is to iterate from 0 to a and count the number of values that satisfies the given equation. We need to traverse only till a since any value greater than a will give the XOR value > a, hence cannot satisfy the equation.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the number of values
// of b such that a = b + (a^b)
#include <bits/stdc++.h>
using namespace std;
  
// function to return the number of solutions
int countSolutions(int a)
{
    int count = 0;
  
    // check for every possible value
    for (int i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
  
    return count;
}
  
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the number of values
// of b such that a = b + (a^b)
  
import java.io.*;
  
class GFG {
  
  
// function to return the number of solutions
 static int countSolutions(int a)
{
    int count = 0;
  
    // check for every possible value
    for (int i = 0; i <= a; i++) {
        if (a == (i + (a ^ i)))
            count++;
    }
  
    return count;
}
  
// Driver Code
  
  
    public static void main (String[] args) {
        int a = 3;
    System.out.println( countSolutions(a));
    }
}
// This code is contributed by inder_verma

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find 
# the number of values of b
# such that a = b + (a^b)
  
# function to return the 
# number of solutions
def countSolutions(a):
  
    count = 0
  
    # check for every possible value
    for i in range(a + 1):
        if (a == (i + (a ^ i))):
            count += 1
  
    return count
  
# Driver Code
if __name__ == "__main__":
    a = 3
    print(countSolutions(a))
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the number of 
// values of b such that a = b + (a^b)
using System;
  
class GFG 
{
  
// function to return the
// number of solutions
static int countSolutions(int a)
{
    int count = 0;
  
    // check for every possible value
    for (int i = 0; i <= a; i++) 
    {
        if (a == (i + (a ^ i)))
            count++;
    }
  
    return count;
}
  
// Driver Code
public static void Main () 
{
    int a = 3;
    Console.WriteLine(countSolutions(a));
}
}
  
// This code is contributed by inder_verma

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the number of 
// values of b such that a = b + (a^b)
  
// function to return the 
// number of solutions
function countSolutions($a)
{
    $count = 0;
  
    // check for every possible value
    for ($i = 0; $i <= $a; $i++) 
    {
        if ($a == ($i + ($a ^ $i)))
            $count++;
    }
  
    return $count;
}
  
// Driver Code
$a = 3;
echo countSolutions($a);
  
// This code is contributed 
// by inder_verma
?>

chevron_right


Output:

4

Time Complexity: O(a)

An Efficient Approach is to observe a pattern of answers when we write the possible solutions for different values of a. Only the set bits are used to determine the number of possible answers. The answer to the problem will always be 2^(number of set bits) which can be determined by observation.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the number of values
// of b such that a = b + (a^b)
#include <bits/stdc++.h>
using namespace std;
  
// function to return the number of solutions
int countSolutions(int a)
{
    int count = __builtin_popcount(a);
  
    count = pow(2, count);
    return count;
}
  
// Driver Code
int main()
{
    int a = 3;
    cout << countSolutions(a);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the number of values
// of b such that a = b + (a^b)
import java.io.*;
class GFG
{
   
    // function to return the number of solutions
    static int countSolutions(int a)
    {
        int count = Integer.bitCount(a);
       
        count =(int) Math.pow(2, count);
        return count;
    }
       
    // Driver Code
        public static void main (String[] args) 
    {
        int a = 3;
        System.out.println(countSolutions(a));
    }
}
// This code is contributed by Raj

chevron_right


Python3

# Python3 program to find the number
# of values of b such that a = b + (a^b)

# function to return the number
# of solutions
def countSolutions(a):

count = bin(a).count(‘1’)
return 2 ** count

# Driver Code
if __name__ == “__main__”:

a = 3
print(countSolutions(a))

# This code is contributed by
# Rituraj Jain

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the number of 
// values of b such that a = b + (a^b)
class GFG
{
  
// function to return the number 
// of solutions
static int countSolutions(int a)
{
    int count = bitCount(a);
  
    count =(int) System.Math.Pow(2, count);
    return count;
}
  
static int bitCount(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}
  
// Driver Code
public static void Main() 
{
    int a = 3;
    System.Console.WriteLine(countSolutions(a));
}
}
  
// This code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find the number of 
// values of b such that a = b + (a^b)
  
// function to return the number 
// of solutions
function countSolutions($a)
{
    $count = bitCount($a);
  
    $count = (int)pow(2, $count);
    return $count;
}
  
function bitCount($n)
{
    $count = 0;
    while ($n != 0)
    {
        $count++;
        $n &= ($n - 1);
    }
    return $count;
}
  
// Driver Code
$a = 3;
echo (countSolutions($a));
  
// This code is contributed by ajit
?>

chevron_right


Output:

4

Time Complexity: O(log N)



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.