-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPowerfull Integer.cpp
66 lines (48 loc) · 1.26 KB
/
Powerfull Integer.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
//{ Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
int powerfullInteger(int n,vector<vector<int>> &intervals,int k){
// Code here
int maxi = 0;
for(int i = 0; i < n; ++i)
maxi = max(maxi, intervals[i][1]);
int ans = -1;
vector<int> freq(maxi + 2, 0);
for(int i = 0; i < n; ++i)
{
++freq[intervals[i][0]];
--freq[intervals[i][1] + 1];
}
int prefSum = 0;
for(int i = 0; i < maxi + 1; ++i)
{
prefSum += freq[i];
if(prefSum >= k)
ans = max(ans, i);
}
return ans;
}
};
//{ Driver Code Starts.
int main(){
int t;
cin>>t;
while(t--){
int n,k;
cin>>n;
vector<vector<int>> intervals(n,vector<int>(2));
for(int i=0;i<n;i++){
cin>>intervals[i][0]>>intervals[i][1];
}
cin>>k;
Solution ob;
cout<<ob.powerfullInteger(n,intervals,k)<<endl;
}
return 0;
}
// } Driver Code Ends