You do not need to print anything. Return a frequency array as an array in the function such that index 0 represents the frequency of 1, index 1 represents the frequency of 2, and so on.
Input: ‘n’ = 6 ‘x’ = 9 ‘arr’ = [1, 3, 1, 9, 2, 7]
Output: [2, 1, 1, 0, 0, 0]
Explanation: Below Table shows the number and their counts, respectively, in the array
Number Count
1 2
2 1
3 1
4 0
5 0
6 0
The first line contains two integers, ‘n’ and ‘x’.
The following line contains ‘n’ integers and the array ‘arr’.
Return the frequency array.
We iterate over ‘1’ to 'n', and for each value in ‘1’ to 'n', we loop over the array ‘arr’ and find the frequency of that element.
We create a frequency array of size 'n'. Initialize with 0 at all the indices.
We traverse the array arr, and for each element in arr, we increment our hashmap at index ‘arr[i] - 1’ by 1.
Instead of creating a new frequency array, we will work around storing the frequency in the given array ‘arr’ itself.
For this, we will utilize the constraint ‘arr[i] >= 1’.
We store the frequencies as negative numbers to distinguish the original elements of the array and the frequencies we have stored.
We iterate through the array ‘arr’, and for each positive element found that is less than equal to n, we look at the index ‘arr[i] - 1’