
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Number of Solutions of n = x + n x using C++
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.
Brute Force Method
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.
Example
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
Example
#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; }
Output
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.
Efficient Approach
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).
Example
#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; }
Output
Number of possible solutions : 4
Complexity of Program
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.
Conclusion
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 Articles
- Find the value of x."\n
- 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 a number x such that sum of x and its digits is equal to given n using C++.
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Find x."\n
- 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\n in C++
- Find the Number of solutions for the equation x + y + z
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Find the values of x and y."\n
- Find the values of x, y, z."\n
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles in C++
- Find minimum x such that (x % k) * (x / k) == n in C++
- In the figure, find the value of $x$."\n
