Count subarrays containing at least K zero
Last Updated :
03 Aug, 2023
Given an array arr[] of N integers and a positive value K, the task is to find the number of subarrays of the given array that contains at least K zero.
Examples:
Input: N = 5, arr = [1, 3, 0, 0, 4], K =1
Output: 14
Explanation: All subarrays containing at least 1 negative element are as follows: [1, 3, 0], [1, 3, 0, 0], [1, 3, 0, 0, 4], [3, 0], [3, 0, 0], [3, 0, 0, 4], [0, 0], [0, 0, 4], [0, 4], [0], [0] these are total 14 subarrays containing at least 1 zero.
Input: N = 5, arr = [1, 3, 1, 1, 4], K = 2
Output: 15
Approach: This can be solved with the following idea:
In this approach we will remove subarrays containing only non-zero integers from the total number of subarrays.
Follow the steps to solve the problem:
- Declare a variable to store the total number of subarrays for a given arr of length N.
- Declare a variable count to store the length of the current subarray containing only non-zero elements and initialize the count with 0.
- Traverse array from index 0 to N-1:
- If the element at the current index is a non-zero increment value of count.
- else, subtract (count*(count+1))/2 from totalSubarrays and set count to 0.
- Subtract (count*(count+1))/2 from totalSubarrays.
- Return totalSubarrays as a result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubarrays( int N, int * arr, int K)
{
int totalSubarrays = (N * (N + 1)) / 2;
int count = 0;
int zeroes = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
zeroes++;
}
if (zeroes < K) {
continue ;
}
if (arr[i] != 0) {
count++;
}
else {
totalSubarrays = totalSubarrays
- ((count * (count + 1)) / 2);
count = 0;
}
}
totalSubarrays
= totalSubarrays - ((count * (count + 1)) / 2);
return totalSubarrays;
}
int main()
{
int arr[] = { 1, 3, 0, 0, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 1;
cout << countSubarrays(N, arr, K);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countSubarrays( int N, int arr[], int K)
{
int totalSubarrays = (N * (N + 1 )) / 2 ;
int count = 0 ;
int zeroes = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 0 ) {
zeroes++;
}
if (zeroes < K) {
continue ;
}
if (arr[i] != 0 ) {
count++;
}
else {
totalSubarrays
= totalSubarrays
- ((count * (count + 1 )) / 2 );
count = 0 ;
}
}
totalSubarrays
= totalSubarrays - ((count * (count + 1 )) / 2 );
return totalSubarrays;
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 0 , 0 , 4 };
int N = arr.length;
int K = 1 ;
System.out.print(countSubarrays(N, arr, K));
}
}
|
Python3
def countSubarrays(N, arr, K):
totalSubarrays = (N * (N + 1 )) / / 2
count = 0
zeroes = 0
for i in range (N):
if arr[i] = = 0 :
zeroes + = 1
if zeroes < K:
continue
if arr[i] ! = 0 :
count + = 1
else :
totalSubarrays - = (count * (count + 1 )) / / 2
count = 0
totalSubarrays - = (count * (count + 1 )) / / 2
return totalSubarrays
arr = [ 1 , 3 , 0 , 0 , 4 ]
N = len (arr)
K = 1
print (countSubarrays(N, arr, K))
|
C#
using System;
class GFG
{
public static int CountSubarrays( int N, int [] arr, int K)
{
int totalSubarrays = (N * (N + 1)) / 2;
int count = 0;
int zeroes = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
{
zeroes++;
}
if (zeroes < K)
{
continue ;
}
if (arr[i] != 0)
{
count++;
}
else
{
totalSubarrays = totalSubarrays - ((count * (count + 1)) / 2);
count = 0;
}
}
totalSubarrays = totalSubarrays - ((count * (count + 1)) / 2);
return totalSubarrays;
}
public static void Main( string [] args)
{
int [] arr = { 1, 3, 0, 0, 4 };
int N = arr.Length;
int K = 1;
Console.Write(CountSubarrays(N, arr, K));
}
}
|
Javascript
function countSubarrays(N, arr, K) {
let totalSubarrays = (N * (N + 1)) / 2;
let count = 0;
let zeroes = 0;
for (let i = 0; i < N; i++) {
if (arr[i] === 0) {
zeroes++;
}
if (zeroes < K) {
continue ;
}
if (arr[i] !== 0) {
count++;
} else {
totalSubarrays = totalSubarrays - (count * (count + 1)) / 2;
count = 0;
}
}
totalSubarrays = totalSubarrays - (count * (count + 1)) / 2;
return totalSubarrays;
}
const arr = [1, 3, 0, 0, 4];
const N = arr.length;
const K = 1;
console.log(countSubarrays(N, arr, K));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...