966. Guess the Number Using Bitwise Questions I

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

The predefined API: int commonSetBits(int num), returns the count of positions where both n and num have a 1 in their binary representation. In other words, it computes the number of set bits in n & num (where & is the bitwise AND operator).

The task is to accurately find and return the value of n by making calls to this API.

Example 1:

Input: n = 31  

Output: 31  

Explanation:

By utilizing the API, it can be confirmed that the hidden number is 31.

Example 2:

Input: n = 33  

Output: 33  

Explanation:

The API can be used to verify that n = 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

If a query is made with num outside this range, the API's response may be unreliable

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

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

Input:

N