Boyer-moore

2 articles

dsa18 min read

Majority Element — Boyer-Moore Voting Algorithm Explained Deeply [LeetCode 169]

The Boyer-Moore Voting Algorithm solves LeetCode 169 in O(n) time and O(1) space using a brilliantly counterintuitive cancellation trick. Learn the proof, the dry run, all four approaches, and why interviewers love this problem — plus the Majority Element II follow-up that extends the same idea to two candidates.

Read →
dsa6 min read

Majority Element II — Extended Boyer-Moore Voting [LC 229]

LeetCode 229 — asked at Amazon, Google, and Microsoft. Find all elements appearing more than n/3 times using the Extended Boyer-Moore Voting Algorithm with two candidates. At most two such elements can exist — verify both candidates with a second pass. O(n) time, O(1) space.

Read →