# Range Queries for Frequencies of array elements

Given an array of n non-negative integers. The task is to find frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type.
Examples:

```Input  : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
left = 2, right = 8, element = 8
left = 2, right = 5, element = 6
Output : 3
1
The element 8 appears 3 times in arr[left-1..right-1]
The element 6 appears 1 time in arr[left-1..right-1]
```

Naive approach: is to traverse from left to right and update count variable whenever we find the element.

Below is the code of Naive approach:-

## C++

 `// C++ program to find total count of an element ` `// in a range ` `#include ` `using` `namespace` `std; ` ` `  `// Returns count of element in arr[left-1..right-1] ` `int` `findFrequency(``int` `arr[], ``int` `n, ``int` `left, ` `                         ``int` `right, ``int` `element) ` `{ ` `    ``int` `count = 0; ` `    ``for` `(``int` `i=left-1; i<=right; ++i) ` `        ``if` `(arr[i] == element) ` `            ``++count; ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Print frequency of 2 from position 1 to 6 ` `    ``cout << ``"Frequency of 2 from 1 to 6 = "` `         ``<< findFrequency(arr, n, 1, 6, 2) << endl; ` ` `  `    ``// Print frequency of 8 from position 4 to 9 ` `    ``cout << ``"Frequency of 8 from 4 to 9 = "` `         ``<< findFrequency(arr, n, 4, 9, 8); ` ` `  `    ``return` `0; ` `} `

## Java

 `// JAVA Code to find total count of an element ` `// in a range ` ` `  `class` `GFG { ` `     `  `    ``// Returns count of element in arr[left-1..right-1] ` `    ``public` `static` `int` `findFrequency(``int` `arr[], ``int` `n,  ` `                                ``int` `left, ``int` `right, ` `                                      ``int` `element) ` `    ``{ ` `        ``int` `count = ``0``; ` `        ``for` `(``int` `i = left - ``1``; i < right; ++i) ` `            ``if` `(arr[i] == element) ` `                ``++count; ` `        ``return` `count; ` `    ``} ` `     `  `    ``/* Driver program to test above function */` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = {``2``, ``8``, ``6``, ``9``, ``8``, ``6``, ``8``, ``2``, ``11``}; ` `        ``int` `n = arr.length; ` `      `  `        ``// Print frequency of 2 from position 1 to 6 ` `        ``System.out.println(``"Frequency of 2 from 1 to 6 = "` `+ ` `             ``findFrequency(arr, n, ``1``, ``6``, ``2``)); ` `      `  `        ``// Print frequency of 8 from position 4 to 9 ` `        ``System.out.println(``"Frequency of 8 from 4 to 9 = "` `+ ` `             ``findFrequency(arr, n, ``4``, ``9``, ``8``)); ` `         `  `    ``} ` `  ``}  ` `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python program to find total   ` `# count of an element in a range ` ` `  `# Returns count of element ` `# in arr[left-1..right-1] ` `def` `findFrequency(arr, n, left, right, element): ` ` `  `    ``count ``=` `0` `    ``for` `i ``in` `range``(left ``-` `1``, right): ` `        ``if` `(arr[i] ``=``=` `element): ` `            ``count ``+``=` `1` `    ``return` `count ` ` `  ` `  `# Driver Code ` `arr ``=` `[``2``, ``8``, ``6``, ``9``, ``8``, ``6``, ``8``, ``2``, ``11``] ` `n ``=` `len``(arr) ` ` `  `# Print frequency of 2 from position 1 to 6 ` `print``(``"Frequency of 2 from 1 to 6 = "``, ` `        ``findFrequency(arr, n, ``1``, ``6``, ``2``)) ` ` `  `# Print frequency of 8 from position 4 to 9 ` `print``(``"Frequency of 8 from 4 to 9 = "``, ` `        ``findFrequency(arr, n, ``4``, ``9``, ``8``)) ` `         `  `     `  `# This code is contributed by Anant Agarwal. `

## C#

 `// C# Code to find total count  ` `// of an element in a range ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Returns count of element  ` `    ``// in arr[left-1..right-1] ` `    ``public` `static` `int` `findFrequency(``int` `[]arr, ``int` `n,  ` `                                    ``int` `left, ``int` `right, ` `                                    ``int` `element) ` `    ``{ ` `        ``int` `count = 0; ` `        ``for` `(``int` `i = left - 1; i < right; ++i) ` `            ``if` `(arr[i] == element) ` `                ``++count; ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `[]arr = {2, 8, 6, 9, 8, 6, 8, 2, 11}; ` `        ``int` `n = arr.Length; ` `     `  `        ``// Print frequency of 2  ` `        ``// from position 1 to 6 ` `        ``Console.WriteLine(``"Frequency of 2 from 1 to 6 = "` `+ ` `                            ``findFrequency(arr, n, 1, 6, 2)); ` `     `  `        ``// Print frequency of 8  ` `        ``// from position 4 to 9 ` `        ``Console.Write(``"Frequency of 8 from 4 to 9 = "` `+ ` `                       ``findFrequency(arr, n, 4, 9, 8)); ` `         `  `    ``} ` `}  ` ` `  `// This code is contributed by Nitin Mittal. `

## PHP

 ` `

Output:

``` Frequency of 2 from 1 to 6 = 1
Frequency of 8 from 4 to 9 = 2
```

Time complexity of this approach is O(right – left + 1) or O(n)
Auxiliary space: O(1)

An Efficient approach is to use hashing. In C++, we can use unordered_map

1. At first, we will store the position in map[] of every distinct element as a vector like that
```  int arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11};
map = {1, 8}
map = {2, 5, 7}
map = {3, 6}
ans so on...```
2. As we can see that elements in map[] are already in sorted order (Because we inserted elements from left to right), the answer boils down to find the total count in that hash map[] using binary search like method.
3. In C++ we can use lower_bound which will returns an iterator pointing to the first element in the range [first, last] which has a value not less than 'left'. and upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than 'right'.
4. After that we just need to subtract the upper_bound() and lower_bound() result to get the final answer. For example, suppose if we want to find the total count of 8 in the range from [1 to 6], then the map of lower_bound() function will return the result 0 (pointing to 2) and upper_bound() will return 2 (pointing to 7), so we need to subtract the both the result like 2 – 0 = 2 .

Below is C++ code of above approach

 `// C++ program to find total count of an element ` `#include ` `using` `namespace` `std; ` ` `  `unordered_map< ``int``, vector<``int``> > store; ` ` `  `// Returns frequency of element in arr[left-1..right-1] ` `int` `findFrequency(``int` `arr[], ``int` `n, ``int` `left, ` `                      ``int` `right, ``int` `element) ` `{ ` `    ``// Find the position of first occurrence of element ` `    ``int` `a = lower_bound(store[element].begin(), ` `                        ``store[element].end(), ` `                        ``left) ` `            ``- store[element].begin(); ` ` `  `    ``// Find the position of last occurrence of element ` `    ``int` `b = upper_bound(store[element].begin(), ` `                        ``store[element].end(), ` `                        ``right) ` `            ``- store[element].begin(); ` ` `  `    ``return` `b-a; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Storing the indexes of an element in the map ` `    ``for` `(``int` `i=0; i

Output:

```Frequency of 2 from 1 to 6 = 1
Frequency of 8 from 4 to 9 = 2
```

This approach will be beneficial if we have a large number of queries of an arbitrary range asking the total frequency of particular element.

Time complexity: O(log N) for single query.

## tags:

Arrays Hash array-range-queries Arrays Hash