-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidingWindow.cpp
50 lines (46 loc) · 1.12 KB
/
slidingWindow.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
#include<bits/stdc++.h>
#include<vector>
using namespace std;
int findMaximum(vector<int> sub)
{
sort(sub.begin(),sub.end());
return sub[sub.size()-1];
}
void printArray(vector<int> pr)
{
for(int i=0;i<pr.size();i++)
{
cout<<pr[i]<<" ";
}
}
void performSliding(vector<int> myArray, int winsize)
{
vector<int> c;
int size = myArray.size();
for(int i=0;i<size;i++)
{
vector<int> subarray;
for(int trav = i; trav<=i+winsize-1;trav++)
{
subarray.push_back(myArray[trav]);
}
int max = findMaximum(subarray);
c.push_back(max);
}
printArray(c);
}
int main()
{
vector<int> a;
int n,x,winsize;
cout<<"Enter size of array and target number!\n";
cin>>n>>winsize;
for(int i=0;i<n;i++)
{
cin>>x;
a.push_back(x);
}
cout<<"Let's perform the sliding window on this array...!\n";
performSliding(a,winsize);
return 0;
}