First Negative Number in Every Window of Size K - Java Solution

1. Introduction

In this blog post, we address a problem that involves finding the first negative number in every window of a given size in an array of integers. This problem is a variant of the sliding window technique and demonstrates how to handle subarrays within a given range.

Problem

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

2. Solution Steps

1. Use a deque (double-ended queue) to keep track of the negative numbers in the current window.

2. Slide the window across the array, adding and removing elements from the deque.

3. Print the front of the deque as the first negative number for each window. If the deque is empty, print 0.

4. Ensure that the deque only contains elements from the current window.

3. Code Program

import java.util.Deque;
import java.util.LinkedList;

public class Solution {

    // Main method for testing
    public static void main(String[] args) {
        int[] a1 = {-5, 1, 2, -6, 9};
        printFirstNegativeNumber(a1, 2);

        int[] a2 = {10, -1, -5, 7, -15, 20, 18, 24};
        printFirstNegativeNumber(a2, 3);
    }

    // Method to print the first negative number in every window of size k
    public static void printFirstNegativeNumber(int[] a, int k) {
        Deque<Integer> deque = new LinkedList<>();

        // Process the first window
        for (int i = 0; i < k; i++) {
            if (a[i] < 0) {
                deque.addLast(i);
            }
        }

        for (int i = k; i < a.length; i++) {
            // Print the first negative number of the previous window
            if (!deque.isEmpty()) {
                System.out.print(a[deque.peek()] + " ");
            } else {
                System.out.print("0 ");
            }

            // Remove the elements which are out of this window
            while (!deque.isEmpty() && deque.peek() <= i - k) {
                deque.removeFirst();
            }

            // Add the current element to the deque if it is negative
            if (a[i] < 0) {
                deque.addLast(i);
            }
        }

        // Print the first negative number for the last window
        if (!deque.isEmpty()) {
            System.out.print(a[deque.peek()]);
        } else {
            System.out.print("0");
        }
        System.out.println();
    }
}

Output:

-5 0 -6 -6
-1 -1 -5 -15 -15 0

Explanation:

The printFirstNegativeNumber method uses a deque to track negative numbers in each sliding window of size k. It prints the first negative number from the deque for each window. If the deque is empty (meaning no negative number in the window), it prints 0

For instance, in the first example with a[] = {-5, 1, 2, -6, 9} and k = 2, the output is -5 (from window {-5, 1}), 0 (from {1, 2}), -6 (from {2, -6}), and -6 (from {-6, 9}).

Comments