- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article we are going to find number of solution of equation n = x + n ⊕ x, i.e we need to find number of values of x possible with given value n such that n = x + n ⊕ x where ⊕ represents XOR operation.

Now we will discuss the complete information regarding number of solutions of n = x + n ⊕ x with an appropriate examples.

We can simple use brute force approach in order to find number of solution, i.e for given value of n we apply every integer value of x starting from 0 and verify whether the equation satisfies or not, value of x should be less than or equals to n because adding value greater than n with (n ⊕ x) will never return n as answer.

Find one value of x for n = 3?

n = x + n ⊕ x Putting x = 0, 3 = 0 + 3 ⊕ 0 3 ⊕ 0 = 3, 3 = 3 LHS = RHS(x = 0 satisfy the equation) So, x = 0 is one of the solution

#include <bits/stdc++.h> using namespace std; int main(){ int n = 3, c=0; for (int x = 0; x <= n; ++x)// loop for giving value of x from 0 to n if (n == x + n ^ x)//checking if value of x satisfies the equation ++c; cout << "Number of possible solutions : " << c; return 0; }

Number of possible solutions : 4

This is simple C++ program to find number of solutions of n = x + n ⊕ x by applying Brute force method.

In this approach, if we look at **n** in the binary form, we need to find the number of bits that are set to 1, and looking at the equation, we can say if n is set, then either x will be set or **n** ⊕ x will be set because 1 ⊕ 1 = 0. It means that n ⊕ x doesn't have it set, so now we can conclude the number of permutations for every set bit in n, i.e., 2^(number of set bits).

#include <bits/stdc++.h> using namespace std; int main (){ int n = 3, no_of_setbits = 0; // initialising n with value and taking count of set bits as 0 while (n != 0){ no_of_setbits = no_of_setbits + (n % 2); // checking if num contains set bit. n = n / 2; } int result = 1 << no_of_setbits; // calculating no. of possible solution with 2^setbits cout << " Number of possible solutions : " << result; return 0; }

Number of possible solutions : 4

The time complexity of this approach is O(n), as we are applying Brute force here. We can apply more efficient methods to improve the efficiency of the program.

In this article, we solve a problem to find a number of solution −

n = x + n ⊕ x. We also learned the C++ program for this problem and the complete approach by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages. Hope you find this article helpful.

- Related Questions & Answers
- Find the Number of solutions for the equation x + y + z <= n using C++
- Count of values of x <= n for which (n XOR x) = (n – x) in C++
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find a number x such that sum of x and its digits is equal to given n using C++.
- Recursive sum of digit in n^x, where n and x are very large in C++
- Find number of solutions of a linear equation of n variables in C++
- Construct a frequency array of digits of the values obtained from x^1, x^2, ....., x^n in C++
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles in C++
- Absolute difference between the first X and last X Digits of N?
- Find a number x such that sum of x and its digits is equal to given n in C++
- C program to generate the value of x power n using recursive function

Advertisements