Monday, June 14, 2021

Published June 14, 2021 by Anonymous with 0 comment

Count negative elements present in every K-length subarray

Given an array arr[] of size N and an integer K, the task is to count the number of negative elements present in all K-length subarrays.

Example:

Input: arr[] = {-1, 2, -2, 3, 5, -7, -5}, K = 3
Output: 2 1 1 1 2
Explanation:
First Subarray: {-1, 2, -2}. Count of negative numbers = 2.
Second Subarray: {2, -2, 3}. Count of negative numbers = 1.
Third Subarray: {-2, 3, 5}. Count of negative numbers = 1.
Fourth Subarray: {3, 5, -7}. Count of negative numbers = 1.
Fifth Subarray: {5, -7, -5}. Count of negative numbers = 2.

Input: arr[] = {-1, 2, 4, 4}, K = 2
Output: 1 0 0

Naive Approach: The simplest approach is to traverse the given array considering every window of size K and find the count of negative numbers in every window.
Time Complexity: O(N*K)
Auxiliary Space: O(1)

Efficient Approach: This problem can be solved using the window sliding technique. Follow the steps below to solve the problem:


  • Initialize a variable count as 0 to store the count of negative elements in a window of size K.
  • Initialize two variables i and j as 0 to store the first and last index of the window respectively.
  • Loop while j<N and perform the following steps:
    • If arr[j] < 0, increment count by 1.
    • If the size of the window, i.e, j-i+1 is equal to K, print the value of count, and check if arr[i] < 0, then decrement count by 1. Also, increment i by 1.
    • Increment the value of j by 1.

Below is the implementation of the above approach

Java

import java.io.*;

import java.util.*;

  

class GFG {

  

    

    

    

    public static void countNegative(int[] arr, int k)

    {

        

        int i = 0;

        int j = 0;

  

        

        int count = 0;

        int n = arr.length;

  

        

        while (j < n) {

  

            

            

            if (arr[j] < 0) {

                count++;

            }

  

            

            if (j - i + 1 == k) {

                System.out.print(count + " ");

  

                

                

                

                if (arr[i] < 0) {

                    count--;

                }

                i++;

            }

  

            j++;

        }

    }

  

    

    public static void main(String[] args)

    {

        

        int[] arr = { -1, 2, -2, 3, 5, -7, -5 };

        int k = 3;

  

        

        countNegative(arr, k);

    }

}

Output:
2 1 1 1 2

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 


Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment