-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximumOccuredInteger..cpp
75 lines (57 loc) · 1.36 KB
/
maximumOccuredInteger..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
63
64
65
66
67
68
69
70
71
72
73
74
75
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
// L and R are input array
// maxx : maximum in R[]
// n: size of array
// arr[] : declared globally with size equal to maximum in L[] and R[]
//Function to find the maximum occurred integer in all ranges.
int maxOccured(int L[], int R[], int n, int maxx){
// Your code here
int freq[maxx+1]={0};
for(int i=0;i<n;i++){
for(int j=L[i];j<=R[i];j++){
freq[j]++;
}
}
// sort(freq.begin(),freq.end());
int largest=0;
for(int i=1;i<maxx+1;i++){
if(freq[i]>freq[largest])
largest=i;
}
return largest;
}
};
// { Driver Code Starts.
int main() {
int t;
//taking testcases
cin >> t;
while(t--){
int n;
//taking size of array
cin >> n;
int L[n];
int R[n];
//adding elements to array L
for(int i = 0;i<n;i++){
cin >> L[i];
}
int maxx = 0;
//adding elements to array R
for(int i = 0;i<n;i++){
cin >> R[i];
if(R[i] > maxx){
maxx = R[i];
}
}
Solution ob;
//calling maxOccured() function
cout << ob.maxOccured(L, R, n, maxx) << endl;
}
return 0;
} // } Driver Code Ends