Dsa
Bit Manipulation
Medium
Gray Code Forensic
A sequence of 2^N integers is intended to be a standard Gray code sequence (where each adjacent pair, including the last and first if cyclic, differs by exactly one bit). However, exactly one element in the sequence was replaced by a wrong integer. Given the sequence, find the index of the corrupted element and the correct value it should have been to restore the 'one-bit-diff' property with its neighbors.
Input Format:
An integer N (the number of bits).
2^N integers representing the corrupted sequence.
Output Format:
Two space-separated integers: Index and CorrectValue.
Example:
Input:
2
0 1 7 2
Output:
2 3
(Explanation: 0->1 (1 bit), 1->7 (2 bits: 111 vs 001), 7->2 (2 bits). Replacing index 2 with 3 (binary 11) restores the sequence: 00->01->11->10.)
Key concepts
bit_manipulationparitygray_code
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