-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_of_SubArray_of_size_K.cpp
84 lines (71 loc) · 1.8 KB
/
max_of_SubArray_of_size_K.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> arr{1, 3, -1, -3, 5, 3, 6, 7}; // input String
int n = arr.size(); // size of input Array
list<int> ls;
vector<int> ans; // vector to store answer string
int k = 3; // size of window
int i, j; // Begin and end Pointers
i = j = 0; // Initial State of Pointers
// Sliding window Loop
while (j < n)
{
// Initial Calculation Step
while (arr[j] > ls.back() && ls.size() > 0)
ls.pop_back();
ls.push_back(arr[j]);
// if Window size didn't hit window size
if (j - i + 1 < k)
j++;
else
{
ans.push_back(ls.front());
if (arr[i] == ls.front())
ls.pop_front();
i++;
j++;
}
}
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
return 0;
}
/*
#include <bits/stdc++.h>
using namespace std;
vector<int> fxn(vector<int> arr, int n, int k)
{
vector<int> answer;
list<int> ls;
int i = 0, j = 0;
while (j < n)
{
while (arr[j] > ls.front() && ls.size() > 0)
ls.pop_back();
ls.push_back(arr[j]);
if (j - i + 1 < k)
j++;
else
{
answer.push_back(ls.front());
if (arr[i] == ls.front())
ls.pop_front();
i++;
j++;
}
}
return answer;
}
int main()
{
vector<int> arr{1, 3, -1, -3, 5, 3, 6, 7}; // input String
int n = arr.size(); // size of input Array
int k = 3; // size of window
vector<int> ans = fxn(arr, n, k);
for (int i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
return 0;
}
*/