-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxEleInRange.cpp
More file actions
61 lines (53 loc) · 964 Bytes
/
MaxEleInRange.cpp
File metadata and controls
61 lines (53 loc) · 964 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
/*
Let say,
L[] = {1, 2, 3} , R[] = {3, 5 , 7}
1. for this line arr[L[i]]++ the array becomes {0,1,1,1,0,0,0,0,……}
2. for this line arr[R[i+1]]– the array becomes {0,1,1,1,-1, 0, -1, 0,-1,……}
3. when we do prefix sum the array becomes {0,1,2,3,2,2,1,1,0…}
*/
int MaxEleRange(vector<int>l,vector<int>r,int n)
{
vector<int>v(1000);
for(int i=0;i<n;i++)
{
v[l[i]]++;
v[r[i]+1]--;
}
int maxe=v[0],res=0;
for(int i=1;i<1000;i++)
{
v[i]+=v[i-1];
if(maxe<v[i])
{
maxe=v[i];
res=i;
}
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//Code here...
int n;cin>>n;
vector<int>l;
vector<int>r;
for(int i=0;i<n;i++)
{
int a;cin>>a;
l.push_back(a);
}
for(int i=0;i<n;i++)
{
int a;cin>>a;
r.push_back(a);
}
int res=MaxEleRange(l,r,n);
cout<<res;
return 0;
}