Dsa
Bit Manipulation
Easy
Precision Permissioning
In a security system, a 'Master Key' (a 32-bit integer) defines required permission bits. A 'Subordinate Key' is 'Aligned' if it has all the bits set that the Master Key has set. Among a list of N Subordinate Keys, find the Aligned key that has the minimum number of 'Extra Bits' (bits set where the Master Key has none). If there's a tie in the number of Extra Bits, choose the Subordinate Key with the smallest numerical value. If no keys are Aligned, output -1.
Example:
Input:
10
3
11 14 31
Output:
11
Explanation:
Master Key 10 (1010).
11 (1011): Aligned (has bits at 8 and 2). Extra bits: bit 0 (1 bit).
14 (1110): Aligned (has bits at 8 and 2). Extra bits: bit 4 (1 bit).
31 (11111): Aligned. Extra bits: bits 0, 4, 16 (3 bits).
Tie between 11 and 14 (both 1 extra bit). Smallest value is 11.
Key concepts
bitwise_andpopcountfiltering
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now