991. Guess the Number Using Bitwise Questions II

The task is to determine an unknown integer n using a special API function.

The predefined API: int commonSetBits(int num), which does the following:

  • It calculates count, which represents the number of positions where both n and num have the same bit (either 0 or 1) in their binary representation.
  • It then updates n using the operation: n = n XOR num, this means n changes every time the function is called.
  • Then it returns the value of count

The task is to determine the initial value of n before any function calls were made.

Example 1:

Input: n = 31  

Output: 31  

Explanation: By utilizing the API, it can be confirmed that the initial value of n was 31.

Example 2:

Input: n = 33  

Output: 33  

Explanation: By utilizing the API, it can be confirmed that the initial value of n was 33.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 230 - 1
  • 0 <= num <= 230 - 1

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Definition of commonBits API.
* int commonBits(int num);
*/
 
class Solution {
public:
int findNumber() {
}
};
 
Test Case

Input:

N