Skip to content

Practice Online Majority Element In Subarray

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1157
LeetCode Problem

Online Majority Element In Subarray

Problem Statement

MajorityChecker(arr) answers query(left,right,threshold) over inclusive indices. Return a value occurring at least threshold times, or -1. The threshold exceeds half the interval length, so at most one value qualifies. Practice approach: A segment tree can merge Boyer-Moore candidate/count summaries to propose a candidate. Store sorted occurrence indices per value and verify its true interval count with two binary searches. Candidate voting alone is insufficient: a candidate may exist without meeting the threshold. Test a strict-majority query that has no qualifying value.

Source: Online Majority Element In Subarray on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["MajorityChecker","query","query","query"]
[[[4,4,7,4,7]],[0,4,3],[1,4,3],[2,2,1]]
Output
[null,4,-1,7]
Explanation

4 occurs three times in the full array but only twice in indices 1 through 4; a one-cell interval returns that cell.

Constraints

1 <= arr.length <= 2 * 10^4
1 <= arr[i] <= 2 * 10^4
0 <= left <= right < arr.length
threshold <= right-left+1 < 2*threshold
At most 10^4 query calls.

Related LeetCode Problems

Practice more problems similar to Online Majority Element In Subarray.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.