-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_the_power_of_k_size_subarray.c++
54 lines (50 loc) · 1.29 KB
/
find_the_power_of_k_size_subarray.c++
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
class Solution {
public:
vector<int> resultsArray(vector<int>& nums, int k) {
vector<int>ans;
bool prev;
bool isgood=true;
for(int i=0;i<k-1;i++){
if(nums[i+1]-nums[i]!=1){
isgood=false;
break;
}
}
if(isgood){
ans.push_back(nums[k-1]);
}else{
ans.push_back(-1);
}
int l=1;
int r=k;
while(r<nums.size()){
if(isgood){
if(nums[r]-nums[l]==k-1){
ans.push_back(nums[r]);
}else{
ans.push_back(-1);
isgood=false;
}
l++;
r++;
}else{
bool check=true;
for(int i=l;i<=r-1;i++){
if(nums[i+1]-nums[i]!=1){
check=false;
break;
}
}
if(check){
isgood=true;
ans.push_back(nums[r]);
}else{
ans.push_back(-1);
}
l++;
r++;
}
}
return ans;
}
};