Skip to content

visual walkthrough

Single Element in a Sorted Array

MediumBinary Search on Index ParityReported at: AmazonMetaMicrosoft+3

Uses index parity to decide which side is broken.

Solve on LeetCode

The idea

Before the single number, every pair starts at an even position. The single number shifts everything after it by one, so pairs then start at odd positions.

Check an even position: if it matches its right neighbour, the single number is further right; if not, it's here or to the left.

Complexity

approachtimespace
Check pair by pairO(n)O(1)
Binary search on pair alignmentO(log n)O(1)

More walkthroughs