-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Subarray.cpp
More file actions
103 lines (81 loc) · 2.05 KB
/
Copy pathArray_Subarray.cpp
File metadata and controls
103 lines (81 loc) · 2.05 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Problem Statement: You are given an array, You have to choose a contiguous subarray of length ‘k’, and find the minimum of that segment, return the maximum of those minimums.
Sample input :
1 → Length of segment x =1
5 → size of space n = 5
1 → space = [ 1,2,3,1,2]
2
3
1
2
Sample output :
3
Explanation :
The subarrays of size x = 1 are [1],[2],[3],[1], and [2],Because each subarray only contains 1 element, each value is minimal with respect to the subarray it is in. The maximum of these values is 3. Therefore, the answer is 3
*/
// #include<iostream>
// #include<vector>
// #include<deque>
// #include<climits>
// using namespace std;
// // Using Slinding Window Technique
// // Need more practice ************************************************
// int main()
// {
// int x,n;
// cin>>x>>n;
// vector<int>arr(n);
// for(int i=0;i<n;i++){
// cin>>arr[i];
// }
// deque<int>dq(n);
// int maxVal = INT_MIN;
// for(int i=0;i<n;i++){
// // For rage
// while(!dq.empty() && dq.front() < i-x+1){
// dq.pop_front();
// }
// // remove greater element
// while(!dq.empty() && arr[dq.back()] >= arr[i]){
// dq.pop_back();
// }
// dq.push_back(i);
// if(i>=x-1){
// maxVal = max(maxVal,arr[dq.front()]);
// }
// }
// cout<<maxVal<<endl;
// return 0;
// }
#include<iostream>
#include<vector>
#include<deque>
#include<climits>
using namespace std;
int main()
{
int x,n;
cin>>x>>n;
vector<int>arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
deque<int>dq(n);
int maxval = INT_MIN;
for(int i=0;i<n;i++){
// Range
while(!dq.empty() && dq.front()<i-x+1){
dq.pop_front();
}
// remove large numbers
while(!dq.empty() && arr[dq.back()] > arr[i]){
dq.pop_back();
}
dq.push_back(i);
if(i>x-1){
maxval = max(maxval,arr[dq.front()]);
}
}
cout<<maxval<<endl;
return 0;
}