-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.cpp
41 lines (36 loc) · 1.06 KB
/
solution.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
class Solution
{
public:
vector<int> resultsArray(vector<int> &nums, int k)
{
int n = nums.size();
vector<int> result;
for (int i = 0; i <= n - k; i++)
{
vector<int> subarray(nums.begin() + i, nums.begin() + i + k);
// Sort the subarray
vector<int> sortedSubarray = subarray;
sort(sortedSubarray.begin(), sortedSubarray.end());
// Check if elements are consecutive
bool isConsecutive = true;
for (int j = 1; j < k; j++)
{
if (sortedSubarray[j] - sortedSubarray[j - 1] != 1)
{
isConsecutive = false;
break;
}
}
// Add the result based on conditions
if (isConsecutive && subarray == sortedSubarray)
{
result.push_back(sortedSubarray.back()); // Max element
}
else
{
result.push_back(-1);
}
}
return result;
}
};