Given a number n, we have to find the number of possible values of X such that n = x + n ? x. Here ? represents XOR
Examples:
Input : n = 3
Output : 4
The possible values of x are 0, 1, 2, and 3.
Input : n = 2
Output : 2
The possible values of x are 0 and 2.
Brute force approach: We can see that x is always equal to or less than n, so we can iterate over the range [0, n] and count the number of values that satisfy the required condition. The time complexity of this approach is O(n).
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfSolutions( int n)
{
int c = 0;
for ( int x = 0; x <= n; ++x)
if (n == x + n ^ x)
++c;
return c;
}
int main()
{
int n = 3;
cout << numberOfSolutions(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int numberOfSolutions( int n)
{
int c = 0 ;
for ( int x = 0 ; x <= n; ++x)
if (n == x + (n ^ x))
++c;
return c;
}
public static void main(String args[])
{
int n = 3 ;
System.out.print(numberOfSolutions(n));
}
}
|
Python3
def numberOfSolutions(n):
c = 0
for x in range (n + 1 ):
if (n = = ( x + ( n ^ x))):
c + = 1
return c
if __name__ = = "__main__" :
n = 3
print (numberOfSolutions(n))
|
C#
using System;
class GFG
{
static int numberOfSolutions( int n)
{
int c = 0;
for ( int x = 0; x <= n; ++x)
if (n == x + (n ^ x))
++c;
return c;
}
public static void Main()
{
int n = 3;
Console.Write(numberOfSolutions(n));
}
}
|
PHP
<?php
function numberOfSolutions( $n )
{
$c = 0;
for ( $x = 0; $x <= $n ; ++ $x )
if ( $n == $x + $n ^ $x )
++ $c ;
return $c ;
}
$n = 3;
echo numberOfSolutions( $n );
|
Javascript
<script>
function numberOfSolutions(n)
{
let c = 0;
for (let x = 0; x <= n; ++x)
if (n == x + n ^ x)
++c;
return c;
}
let n = 3;
document.write(numberOfSolutions(n));
</script>
|
Time complexity: O(n)
Auxiliary Space: O(1)
Efficient approach: We can solve this problem in a more efficient way if we consider n in its binary form. If a bit of n is set, i.e. 1, then we can deduce that there must be a corresponding set bit in either x or n ? x (but not both). If the corresponding bit is set in x, then it is not set in n ? x as 1 ? 1 = 0. Otherwise the bit is set in n ? x as 0 ? 1 = 1. Therefore for every set bit in n, we can have either a set bit or an unset bit in x. However, we cannot have a set bit in x corresponding to an unset bit in n. By this logic, the number of solutions comes out to be 2 raised to the power of the number of set bits in n. The time complexity of this approach is O(log n).
C++
#include <bits/stdc++.h>
using namespace std;
int numberOfSolutions( int n)
{
int c = 0;
while (n) {
c += n % 2;
n /= 2;
}
return pow (2, c);
}
int main()
{
int n = 3;
cout << numberOfSolutions(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int numberOfSolutions( int n)
{
int c = 0 ;
while (n> 0 ) {
c += n % 2 ;
n /= 2 ;
}
return ( int )Math.pow( 2 , c);
}
public static void main (String[] args) {
int n = 3 ;
System.out.println( numberOfSolutions(n));
}
}
|
Python3
from math import *
def numberOfSolutions(n) :
c = 0
while (n) :
c + = n % 2
n / / = 2
return int ( pow ( 2 , c))
if __name__ = = "__main__" :
n = 3
print (numberOfSolutions(n))
|
C#
using System;
class GFG
{
static int numberOfSolutions( int n)
{
int c = 0;
while (n > 0)
{
c += n % 2;
n /= 2;
}
return ( int )Math.Pow(2, c);
}
public static void Main ()
{
int n = 3;
Console.WriteLine(numberOfSolutions(n));
}
}
|
PHP
<?php
function numberOfSolutions( $n )
{
$c = 0;
while ( $n )
{
$c += $n % 2;
$n /= 2;
}
return pow(2, $c );
}
$n = 3;
echo numberOfSolutions( $n );
?>
|
Javascript
<script>
function numberOfSolutions(n)
{
let c = 0;
while (n > 0) {
c += n % 2;
n = parseInt(n / 2, 10);
}
return Math.pow(2, c);
}
let n = 3;
document.write(numberOfSolutions(n));
</script>
|
Time complexity: O(log n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!