Back to Practice Dashboard
DSA SectionEasy
Boyer Moore Voting
Learn how to solve the 'Boyer Moore Voting' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Write a function boyer_moore_majority(arr) that implements the Boyer-Moore Majority Vote Algorithm to find the majority element (the element that appears more than n // 2 times) in a list of integers arr. If no majority element exists, return -1.
Constraints
- •1 <= len(arr) <= 5 * 10^4
- •-10^9 <= arr[i] <= 10^9
Examples
Example 1
Input
arr = [1, 1, 1, 2, 2]
Output
1
Explanation
1 appears 3 times which is greater than 5 // 2 = 2 times.
Example 2
Input
arr = [1, 2, 3]
Output
-1
Explanation
No element appears more than 3 // 2 = 1 time.
Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.