forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE0057.cpp
More file actions
45 lines (39 loc) · 933 Bytes
/
E0057.cpp
File metadata and controls
45 lines (39 loc) · 933 Bytes
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
// Problem Code: MFREQ
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> Left, Right;
void preprocess(vector<int> A) {
Left = vector<int>(A.size());
Right = vector<int>(A.size());
Left[0] = 0;
Right[A.size()-1] = A.size()-1;
for(int i=1 ; i<A.size() ; i++)
Left[i] = (A[i] == A[i-1]) ? Left[i-1] : i;
for(int i=A.size()-2 ; i>=0 ; i--)
Right[i] = (A[i] == A[i+1]) ? Right[i+1] : i;
}
int mostFrequentElement(int L, int R, int k, vector<int> &A) {
int mid, freq;
mid = (L + R) / 2;
freq = min(Right[mid], R) - max(Left[mid], L) + 1;
return (freq < k) ? -1 : A[mid];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M, L, R, k, num;
vector<int> A;
cin >> N >> M;
for(int i=0 ; i<N ; i++) {
cin >> num;
A.push_back(num);
}
preprocess(A);
while(M--) {
cin >> L >> R >> k;
cout << mostFrequentElement(L-1, R-1, k, A) << endl;
}
return 0;
}