Imagine you're playing an interactive guessing game where you need to find a secret number n between 0 and 230 - 1. However, there's a twist that makes this challenge particularly intriguing!
You have access to a special API function commonBits(num) that helps you gather information about the secret number. But here's the catch: every time you call this function, the secret number changes! Your mission is to find the original value of n before any modifications.
The commonBits(num) function works as follows:
- It calculates how many bit positions have the same value in both
nand your inputnum - It then performs
n = n XOR num(modifying the secret number!) - Finally, it returns the count of matching bits
Goal: Determine the initial value of the secret number n using strategic calls to commonBits().
Note: Only the first 30 bits are considered when counting common bits, as all numbers are guaranteed to be less than 230.
Input & Output
Visualization
Time & Space Complexity
We make exactly 30 calls (one for each bit position)
Only need to store the result and bit position
Constraints
- 0 โค n โค 230 - 1
- You can make at most 30 calls to commonBits()
- Only the first 30 bits are considered for bit comparison