-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreqOfArrayElementsInRange.cpp
62 lines (48 loc) · 1.29 KB
/
freqOfArrayElementsInRange.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Given an array A[] of N positive integers which can contain integers from 1 to P where elements can be repeated or can be absent from the array. Your task is to count the frequency of all elements from 1 to N.
// Note: The elements greater than N in the array can be ignored for counting.
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
//Function to count the frequency of all elements from 1 to N in the array.
void frequencyCount(vector<int>& arr,int N, int P)
{
// code here
int temp[100001]={0};
for(int i=0;i<N;i++){
temp[arr[i]]++;
}
for(int j=1;j<=N;j++){
arr[j-1]=temp[j];
}
}
};
// { Driver Code Starts.
int main()
{
long long t;
//testcases
cin >> t;
while(t--){
int N, P;
//size of array
cin >> N;
vector<int> arr(N);
//adding elements to the vector
for(int i = 0; i < N ; i++){
cin >> arr[i];
}
cin >> P;
Solution ob;
//calling frequncycount() function
ob.frequencyCount(arr, N, P);
//printing array elements
for (int i = 0; i < N ; i++)
cout << arr[i] << " ";
cout << endl;
}
return 0;
}
// } Driver Code Ends