QUESTION
Given an array and a positive integer k, find the first negative integer for each and every window(contiguous subarray) of size k. If a window does not contain a negative interger, then print 0 for that window.
“TESTCASE_1”: “8\n12 -1 -7 8 -15 30 16 28\n###—###SEPERATOR—###—\n-1 -1 -7 -15 -15 0”, “TESTCASE_2”: “6\n-9 -6 5 1 0 -8\n###—###SEPERATOR—###—\n-9 -6 0 -8”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0
ANSWER
#include <iostream>
#include<climits>
#include<deque>
using namespace std;
void print(int arr[],int n,int k)
{
deque<int> d;
for(int i=0;i<k;i++)
if(arr[i]<0)
d.push_back(i);
for(int i=k;i<n;i++)
{
if(!d.empty())
cout<<arr[d.front()]<<" ";
else
cout<<"0 ";
while(!d.empty() && d.front()<(i-k+1))
d.pop_front();
if(arr[i]<0)
d.push_back(i);
}
if(!d.empty())
cout<<arr[d.front()]<<" ";
else
cout<<"0 ";
}
int main()
{
int Length;
cin>>Length;
int arr[Length];
for(int i=0;i<Length;i++)
cin>>arr[i];
print(arr,Length,3);
cout<<endl;
return 0;
}