Count negative elements present in every K-length subarray
Last Updated :
15 Dec, 2022
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
C++
#include<bits/stdc++.h>
using namespace std;
void countNegative(vector< int > arr, int k)
{
int i = 0;
int j = 0;
int count = 0;
int n = arr.size();
while (j < n)
{
if (arr[j] < 0)
{
count++;
}
if (j - i + 1 == k)
{
cout << count << " " ;
if (arr[i] < 0)
{
count--;
}
i++;
}
j++;
}
}
int main()
{
vector< int > arr{ -1, 2, -2, 3, 5, -7, -5 };
int k = 3;
countNegative(arr, k);
}
|
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);
}
}
|
Python3
def countNegative(arr,k):
i = 0
j = 0
count = 0
n = len (arr)
while (j < n):
if (arr[j] < 0 ):
count = count + 1
if (j - i + 1 = = k):
print (count,end = " " )
if (arr[i] < 0 ):
count = count - 1
i = i + 1
j = j + 1
arr = [ - 1 , 2 , - 2 , 3 , 5 , - 7 , - 5 ]
k = 3
countNegative(arr, k)
|
C#
using System;
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)
{
Console.Write(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);
}
}
|
Javascript
<script>
function countNegative(arr, k)
{
var i = 0;
var j = 0;
var count = 0;
var n = arr.length;
while (j < n)
{
if (arr[j] < 0)
{
count++;
}
if (j - i + 1 == k)
{
document.write( count + " " );
if (arr[i] < 0)
{
count--;
}
i++;
}
j++;
}
}
var arr = [ -1, 2, -2, 3, 5, -7, -5 ];
var k = 3;
countNegative(arr, k);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...