Can you implement an efficient C++ code for finding frequency of an element for a range into an array?

I am a beginner in competitive programming.
I've read a problem where I have to output the number of frequency of an element into a sub-array range.

I've implemented that using normal for loop and counting the frequency. But I got TLE. I need an efficient function for it. Can you please write the C++ implementation for me? Its only for my learning purpose.

Here the the problem sample:

Input an array of N;
Then, enter number of queries q,
After that, in each line there will be given a sub array range i,j and an element e;
You have to find the frequency of element e into the sub-array.

Note: The array & query range may be very large

Example:

10
4 1 6 4 7 9 1 3 2 6
3
1 5 4
2
3 10 1
1
2 8 7
1
Last edited on
suppose that you store or every number in the array the indexes where it appears
By instance, for "4 1 6 4 7 9 1 3 2 6" you'll have index[1] = {1, 6}
with this you can easily compute the accumulate frequency over the array, and to find it in the range is as simple as accumulate[b] - accumulate[a]
Topic archived. No new replies allowed.