forked from NIKHILSINGH1510/Test_Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumofsubarray.cpp
More file actions
58 lines (47 loc) · 1.32 KB
/
maximumofsubarray.cpp
File metadata and controls
58 lines (47 loc) · 1.32 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
//Maximum of Sub array in an given array
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define t int
#define in cin
#define ff vector<int>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vp(it,arr) for(auto it:arr){cout<<it<<;}
#define LOOP(i,a,b) for(int i=a;i<b;i++)
using namespace std;
void print_max_in_all_subarray(int arr[],int k,int n){
cout<<n<<endl;
deque<int>arru;
arru.push_front(0);
for(int i=1;i<k;i++){
if(arr[i]>arr[arru.front()]){
arru.pop_back();
arru.push_back(i);
}else{
arru.push_back(i);
}
}
for(int c=k;c<n;c++){
cout<<arr[arru.front()]<<" ";
if(arr[c]>arr[arru.front()]){
arru.pop_back();
while(arr[arru.front()]<arr[c]&&arru.empty()==false){
arru.pop_back();
}
arru.push_back(c);
}else{
arru.push_back(c);
}
if(arru.front()<=c-k){
arru.pop_front();
}
}
cout<<arr[arru.front()]<<endl;
return;
}
int main(){
int arru[]={1,2,3,1,4,5,2,3,6};
int n=(sizeof(arru)/sizeof(arru[0]));
print_max_in_all_subarray(arru,3,n);
return 0;
}