You are given an integer array arr of length n and a 2D array queries, where each query is represented as [l, r, limit].
For each query:
• Consider the subarray arr[l...r].
• Find the element that appears at least limit times.
• If multiple elements satisfy this condition, choose the one with the highest frequency.
• If there’s still a tie, choose the smallest element.
• If no element meets the condition, return -1 for that query.
Return an array result where result[i] corresponds to the answer for the i-th query.
Input: arr = [4, 5, 4, 6, 4, 5, 5] queries = [[0, 6, 3], [1, 4, 2], [2, 5, 2]]
Output: [4, 4, 5]
Input: arr = [2, 3, 2, 3, 2, 4, 3, 2] queries = [[0, 7, 4], [1, 5, 2], [3, 6, 2], [2, 4, 3]]
Output: [2, 3, 3, 2]
Accepted:
Submission: