Problem Description
Design a data structure MajorityChecker that efficiently finds the majority element for a given subarray. The majority element of a subarray is an element that occurs at least threshold times in the subarray. If no such element exists, return -1.
Key Insights
- Preprocess the array by storing the indices of each element to allow fast frequency counting using binary search.
- Given the constraint 2*threshold > subarray length, there can be at most one valid candidate which simplifies verification.
- During each query, randomly sample a few indices from the subarray to retrieve candidate elements and then verify their frequencies.
- Binary search on the candidate's index list gives a fast count of its occurrences in the queried range.
Space and Time Complexity
Time Complexity: O(20 * log(n)) per query in worst-case due to the constant number of samples (20) and binary searches for each candidate. Space Complexity: O(n), where n is the size of the array, used for storing indices for each number.
Solution
We preprocess the input array by constructing a map from element values to their list of indices. For each query on subarray [left, right] with a given threshold:
- Use random sampling (e.g., 20 samples) to pick candidate elements from the subarray.
- For each candidate, use binary search on its list of indices to determine how many times it appears in the subarray.
- If the candidate's frequency meets or exceeds the threshold, return that candidate.
- If no candidate passes the threshold, return -1. This approach exploits the statistical likelihood, enhanced by the constraints, that a valid majority candidate will be sampled.