First negative in every window of size k

 Given an array arr[]  and a positive integer k, find the first negative integer for each and every window(contiguous subarray) of size k.


Note: If a window does not contain a negative integer, then return 0 for that window.


Examples:


Input: arr[] = [-8, 2, 3, -6, 10] , k = 2

Output: [-8, 0, -6, -6]

Explanation:

Window [-8, 2] First negative integer is -8.

Window [2, 3] No negative integers, output is 0.

Window [3, -6] First negative integer is -6.

Window [-6, 10] First negative integer is -6.

Input: arr[] = [12, -1, -7, 8, -15, 30, 16, 28] , k = 3

Output: [-1, -1, -7, -15, -15, 0] 

Explanation:

Window [12, -1, -7] First negative integer is -1.

Window [-1, -7, 8] First negative integer is -1.

Window [-7, 8, -15] First negative integer is -7.

Window [8, -15, 30] First negative integer is -15.

Window [-15, 30, 16] First negative integer is -15.

Window [30, 16, 28] No negative integers, output is 0.

Input: arr[] = [12, 1, 3, 5] , k = 3

Output: [0, 0] 

Explanation:

Window [12, 1, 3] No negative integers, output is 0.

Window [1, 3, 5] No negative integers, output is 0.



BruteForce -Problem (Time Limit Exceeded)


class Solution {
    static List<Integer> firstNegInt(int arr[], int k) {
        // write code here
       
           
        int n=arr.length;
    //int [] result= new int [n-k+1];
   List<Integer> result = new ArrayList<>();
       
       
        for(int i=0;i<=n-k;i++)
        {
            int neg=0;
           
            for(int j=i;j<i+k;j++)
            {
                if(arr[j] <0){
                   
                    neg=arr[j];
                    break;
                }
             
             
            }
            result.add(neg);
        }
        return result;
       
    }
}


Optimised Solution - Using Queue


import java.util.*;

class Solution {
    static List<Integer> firstNegInt(int arr[], int k) {
        List<Integer> result = new ArrayList<>();
        Queue<Integer> q = new LinkedList<>();
        int n = arr.length;

        for (int i = 0; i < n; i++) {
            // Step 1: Store index of negative element
            if (arr[i] < 0) {
                q.offer(i);
            }

            // Step 2: Remove indices out of current window
            if (!q.isEmpty() && q.peek() < i - k + 1) {
                q.poll();
            }

            // Step 3: Store result if we have a complete window
            if (i >= k - 1) {
                if (!q.isEmpty()) {
                    result.add(arr[q.peek()]);
                } else {
                    result.add(0);
                }
            }
        }

        return result;
    }

    public static void main(String[] args) {
        int[] arr = { -8, 2, 3, -6, 10 };
        int k = 2;
        System.out.println(firstNegInt(arr, k)); // Output: [-8, 0, -6, -6]
    }
}



Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence