-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathFirst negative integer in every window of size k.cpp
72 lines (60 loc) · 1.58 KB
/
First negative integer in every window 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
/*
First negative integer in every window of size k
==================================================
Given an array A[] of size N and a positive integer K, find the first negative integer for each and every window(contiguous subarray) of size K.
Example 1:
Input :
N = 5
A[] = {-8, 2, 3, -6, 10}
K = 2
Output :
-8 0 -6 -6
Explanation :
First negative integer for each window of size k
{-8, 2} = -8
{2, 3} = 0 (does not contain a negative integer)
{3, -6} = -6
{-6, 10} = -6
Example 2:
Input :
N = 8
A[] = {12, -1, -7, 8, -15, 30, 16, 28}
K = 3
Output :
-1 -1 -7 -15 -15 0
Your Task:
You don't need to read input or print anything. Your task is to complete the function printFirstNegativeInteger() which takes the array A[], its size N and an integer K as inputs and returns the first negative number in every window of size K starting from the first till the end. If a window does not contain a negative integer , then return 0 for that window.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(K)
Constraints:
1 <= N <= 105
1 <= A[i] <= 105
1 <= K <= N
*/
#define ll long long
vector<ll> printFirstNegativeInteger(ll arr[], ll n, ll k)
{
vector<ll> ans;
queue<int> idx;
for (int i = 0; i < k; ++i)
{
if (arr[i] < 0)
idx.push(i);
}
if (idx.size())
ans.push_back(arr[idx.front()]);
else
ans.push_back(0);
for (int i = k; i < n; ++i)
{
while (idx.size() && idx.front() <= i - k)
idx.pop();
if (arr[i] < 0)
idx.push(i);
if (idx.size())
ans.push_back(arr[idx.front()]);
else
ans.push_back(0);
}
return ans;
}