TomoLink
CompaniesMetaData Structures & AlgorithmsOnline Majority Element In Subarray
DSA
HardArray

Online Majority Element In Subarray

arraybinary searchdesign

Problem Statement

Design a data structure that efficiently finds the majority element of a given subarray.

The majority element of a subarray is an element that occurs threshold times or more in the subarray.

Implementing the MajorityChecker class:

MajorityChecker(int[] arr) Initializes the instance of the class with the given array arr .

int query(int left, int right, int threshold) returns the element in the subarray arr[left...right] that occurs at least threshold times, or -1 if no such element exists.

Examples

Example 1
Input:
Output:

Constraints

1 <= arr.length <= 2 * 10 ^4
1 <= arr[i] <= 2 * 10 ^4
0 <= left <= right < arr.length
threshold <= right - left + 1
2 * threshold > right - left + 1
At most 10 ^4 calls will be made to query .
😤
Hard
Difficulty
Topic Info
ModuleDSA
CategoryArray
Sub-topicBinary Search
Tags
arraybinary searchdesignbinary indexed treesegment tree
Navigation
Online Majority Element In Subarray [Hard] | Meta Dsa | TomoLink