Threshold Majority Queries

You are given an integer array nums of length n and an array queries, where queries[i] = [li, ri, thresholdi].

Return an array of integers ans where ans[i] is equal to the element in the subarray nums[li...ri] that appears at least thresholdi times, selecting the element with the highest frequency and choosing the smallest element in case of a tie, or -1 if no such element exists.

Example 1
Inputnums = [1,1,2,2,1,1], queries = [[0,5,4],[0,3,3],[2,3,2]]
Output[1,-1,2]
For the three queries, the qualifying answers are 1, no value reaching the threshold, and 2, respectively.
Example 2
Inputnums = [3,2,3,2,3,2,3], queries = [[0,6,4],[1,5,2],[2,4,1],[3,3,1]]
Output[3,2,3,2]
For the four queries, the highest-frequency qualifying elements are 3, 2, 3, and 2, respectively.

Constraints

  • 1 <= nums.length == n <= 10^4
  • 1 <= nums[i] <= 10^9
  • 1 <= queries.length <= 5 * 10^4
  • queries[i] = [li, ri, thresholdi]
  • 0 <= li <= ri < n
  • 1 <= thresholdi <= ri - li + 1

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate