Imagine you're playing a digital guessing game where you need to find a hidden number n. However, instead of asking "Is it higher or lower?", you can only ask bitwise questions!
You have access to a special API function: int commonSetBits(int num)
This function tells you how many bit positions have 1s in both the hidden number n and your guess num. In other words, it returns the count of set bits in n & num (bitwise AND).
Your mission: Use this API strategically to discover the hidden number n.
Example: If the hidden number is n = 6 (binary: 110):
โข commonSetBits(4) returns 1 because 6 & 4 = 110 & 100 = 100 (one set bit)
โข commonSetBits(7) returns 2 because 6 & 7 = 110 & 111 = 110 (two set bits)
Input & Output
Visualization
Time & Space Complexity
We may need to test up to n numbers in the worst case, where n is the target value
Only using a constant amount of extra space for variables
Constraints
- 1 โค n โค 230
- The hidden number is a positive integer
- Interactive problem: You must call the API efficiently
- Maximum 50 calls to commonSetBits allowed