Skip to content

Practice Range Frequency Queries

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

Medium#2080
LeetCode Problem

Range Frequency Queries

Problem Statement

RangeFreqQuery(arr) stores a fixed zero-based array. query(left,right,value) returns how often value appears between the inclusive endpoints left and right. Practice approach: Build a sorted list of occurrence indices for each value while scanning the array. Use two binary searches to count indices at least left and at most right. Preprocessing and storage are O(n), while each query takes O(log m) for m occurrences of the requested value. An absent value returns zero. Test single-index intervals and endpoint matches.

Source: Range Frequency Queries on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["RangeFreqQuery","query","query","query"]
[[[4,7,4,4]],[1,3,4],[0,0,4],[0,3,9]]
Output
[null,2,1,0]
Explanation

The requested value occurs twice in positions 1 through 3, once at position 0, and 9 is absent.

Constraints

1 <= arr.length <= 10^5
1 <= arr[i], value <= 10^4
0 <= left <= right < arr.length
At most 10^5 query calls.

Related LeetCode Problems

Practice more problems similar to Range Frequency Queries.

Review your practice with PhantomCodeAI

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