Problem statement
Given an array and a window size k, report the first negative value inside every window of k consecutive values. A window with no negative reports 0.
Only negatives matter, so the deque holds their indices, oldest at the front. The earliest negative still inside the window is the answer, and one that has left it can never come back.
Examples
Input[12, -1, -7, 8, -2, 5], k = 3
Output[-1, -1, -7, -2]
The third window is -7, 8, -2, so the earliest negative is -7.
Input[3, 4, 5, 6], k = 2
Output[0, 0, 0]
No window holds a negative, so every answer is 0.
The walkthrough is part of Premium
The statement and the examples above stay free. The full how-to-solve walkthrough, the cost notes in plain words and the interactive step-by-step visualizer unlock with a Premium plan.
See pricing