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