Assume you are a student studying in school.You are given a task to find first negative integer for each and every window of size k.
Input Description:
First line contains an integer n denoting the size of the array. The next line contains n space separated integers forming the array. The last line contains the window size k.
Output Description:
Print the first negative integer in that window.If all the numbers are positive print 0
Sample Input :
7
1 -2 -3 -4 5 6 -7
3
Sample Output :
-2 -2 -3 -4 -7
Code:
from collections import deque
def printFirstNegativeInteger(arr, n, k):
Di = deque()
for i in range(k):
if (arr[i] < 0):
Di.append(i);
for i in range(k, n):
if (not Di):
print(0, end = ' ')
else:
print(arr[Di[0]], end = ' ')
while Di and Di[0] <= (i - k):
Di.popleft()
if (arr[i] < 0):
Di.append(i)
if not Di:
print(0)
else:
print(arr[Di[0]], end = " ")
n=int(input())
arr=list(map(int,input().split(' ')))
k=int(input())
printFirstNegativeInteger(arr, n, k)
Private Testcase #1
Testcase Status:
>>> error
Input:
7
1 -2 -3 -4 5 6 -7
3
Expected Output:
-2 -2 -3 -4 -7
Actual Output:
-2 -2 -3 -4 -7
Execution Time:
>>> 0.025s