03. Majority Vote
The problem can be found at the following link: Question Link
Problem Description
You are given an array of integers, nums[], where each number represents a vote for a candidate. Return the candidates that have votes greater than one-third of the total votes. If there’s no majority vote, return -1.
Example:
Input:
nums = [2, 1, 5, 5, 5, 5, 6, 6, 6, 6, 6]Output:
[5, 6]Explanation: 5 and 6 occur more than n/3 times.
Input:
nums = [1, 2, 3, 4, 5]Output:
[-1]Explanation: No candidate occurs more than n/3 times.
My Approach
Candidate Selection:
Use the Boyer-Moore Voting Algorithm to identify potential candidates that could be a majority, tracking two candidates and their counts.
Validation:
After identifying candidates, validate their counts in the original array to check if they occur more than n/3 times.
Result Compilation:
Store candidates that meet the criteria and return them; if none meet the criteria, return -1.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we traverse the array a constant number of times to find and validate candidates.
Expected Auxiliary Space Complexity: O(1), as we use a constant amount of additional space to store candidate information.
Code (C++)
Code (Java)
Code (Python)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit my LinkedIn: Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
⭐ Star this repository if you find it helpful or intriguing! ⭐
📍Visitor Count
Last updated