> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/stacks-and-queues/first-negative-integer-in-every-window-of-size-k.md).

# First negative integer in every window of size k

Given an array and a positive integer k, find the first negative integer for each window(contiguous subarray) of size k. If a window does not contain a negative integer, then print 0 for that window.

**Examples:**

```
Input : arr[] = {-8, 2, 3, -6, 10}, k = 2
Output : -8 0 -6 -6
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

Input : arr[] = {12, -1, -7, 8, -15, 30, 16, 28} , k = 3
Output : -1 -1 -7 -15 -15 0 
```

```java
public class Solution {
    public int[] solve(int[] A, int B) {
        Queue<Integer> q = new LinkedList<>();
        for (int i = 0; i < B; i++) {
            if (A[i] < 0)
                q.add(i);
        }
        int[] ans = new int[A.length - B + 1];
        ans[0] = q.size() > 0 ? A[q.peek()] : 0;
        for (int i = B; i < A.length; i++) {
            if (A[i] < 0)
                q.add(i);
            if (q.size() > 0 && q.peek() <= i - B)
                q.poll();
            ans[i - B + 1] = q.size() > 0 ? A[q.peek()] : 0;
        }
        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/stacks-and-queues/first-negative-integer-in-every-window-of-size-k.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
