Open In App

Count subarrays containing at least K zero

Last Updated : 03 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ code to implement the approch
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count number of subarrays
// containing atleast 1 negative integer
int countSubarrays(int N, int* arr, int K)
{
 
    // Variable for total possible number
    // of subarrays for given
    // array of size number
    int totalSubarrays = (N * (N + 1)) / 2;
 
    // Count variable to find length of
    // each subarray containing only
    // non-zero integers
    int count = 0;
    int zeroes = 0;
 
    // Traverse through array from
    // left to right
    for (int i = 0; i < N; i++) {
        if (arr[i] == 0) {
            zeroes++;
        }
 
        if (zeroes < K) {
            continue;
        }
 
        // If current element is not zero,
        // increment value of count
        if (arr[i] != 0) {
            count++;
        }
        else {
 
            // Otherwise subtract number
            // of subarrays due to this
            // subarray of non-zero
            // integers form totalSubarrays
            totalSubarrays = totalSubarrays
                             - ((count * (count + 1)) / 2);
 
            // Reset value of count to 0
            count = 0;
        }
    }
 
    // Subtract number of subarrays due
    // to this subarray of non-zero
    // integers form totalSubarrays
    totalSubarrays
        = totalSubarrays - ((count * (count + 1)) / 2);
 
    // Return totalSubarrays as answer
    return totalSubarrays;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 0, 0, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 1;
 
    // Function Call
    cout << countSubarrays(N, arr, K);
    return 0;
}


Java




// Java code to implement the approch
import java.io.*;
 
class GFG {
    // Function to count number of subarrays
    // containing atleast 1 negative integer
    public static int countSubarrays(int N, int arr[], int K)
    {
 
        // Variable for total possible number
        // of subarrays for given
        // array of size number
        int totalSubarrays = (N * (N + 1)) / 2;
 
        // Count variable to find length of
        // each subarray containing only
        // non-zero integers
        int count = 0;
        int zeroes = 0;
 
        // Traverse through array from
        // left to right
        for (int i = 0; i < N; i++) {
            if (arr[i] == 0) {
                zeroes++;
            }
 
            if (zeroes < K) {
                continue;
            }
 
            // If current element is not zero,
            // increment value of count
            if (arr[i] != 0) {
                count++;
            }
            else {
 
                // Otherwise subtract number
                // of subarrays due to this
                // subarray of non-zero
                // integers form totalSubarrays
                totalSubarrays
                    = totalSubarrays
                      - ((count * (count + 1)) / 2);
 
                // Reset value of count to 0
                count = 0;
            }
        }
 
        // Subtract number of subarrays due
        // to this subarray of non-zero
        // integers form totalSubarrays
        totalSubarrays
            = totalSubarrays - ((count * (count + 1)) / 2);
 
        // Return totalSubarrays as answer
        return totalSubarrays;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 3, 0, 0, 4 };
        int N = arr.length;
        int K = 1;
 
        // Function Call
        System.out.print(countSubarrays(N, arr, K));
    }
}
 
// This code is contributed by Rohit Pradhan


Python3




# Function to count number of subarrays
# containing at least 1 negative integer
def countSubarrays(N, arr, K):
 
    # Variable for total possible number
    # of subarrays for given
    # array of size number
    totalSubarrays = (N * (N + 1)) // 2
 
    # Count variable to find length of
    # each subarray containing only
    # non-zero integers
    count = 0
    zeroes = 0
 
    # Traverse through array from
    # left to right
    for i in range(N):
        if arr[i] == 0:
            zeroes += 1
 
        if zeroes < K:
            continue
 
        # If current element is not zero,
        # increment value of count
        if arr[i] != 0:
            count += 1
        else:
 
            # Otherwise subtract number
            # of subarrays due to this
            # subarray of non-zero
            # integers form totalSubarrays
            totalSubarrays -= (count * (count + 1)) // 2
 
            # Reset value of count to 0
            count = 0
 
    # Subtract number of subarrays due
    # to this subarray of non-zero
    # integers form totalSubarrays
    totalSubarrays -= (count * (count + 1)) // 2
 
    # Return totalSubarrays as answer
    return totalSubarrays
 
# Driver code
arr = [1, 3, 0, 0, 4]
N = len(arr)
K = 1
 
# Function Call
print(countSubarrays(N, arr, K))
 
#contributed by Tushar Rokade


C#




// c# code to implement the approch
 
using System;
 
class GFG
{
    // Function to count number of subarrays
    // containing atleast 1 negative integer
    public static int CountSubarrays(int N, int[] arr, int K)
    {
        // Variable for total possible number
        // of subarrays for given
        // array of size number
        int totalSubarrays = (N * (N + 1)) / 2;
 
        // Count variable to find length of
        // each subarray containing only
        // non-zero integers
        int count = 0;
        int zeroes = 0;
 
        // Traverse through array from
        // left to right
        for (int i = 0; i < N; i++)
        {
            if (arr[i] == 0)
            {
                zeroes++;
            }
 
            if (zeroes < K)
            {
                continue;
            }
 
            // If current element is not zero,
            // increment value of count
            if (arr[i] != 0)
            {
                count++;
            }
            else
            {
                // Otherwise subtract number
                // of subarrays due to this
                // subarray of non-zero
                // integers form totalSubarrays
                totalSubarrays = totalSubarrays - ((count * (count + 1)) / 2);
 
                // Reset value of count to 0
                count = 0;
            }
        }
 
        // Subtract number of subarrays due
        // to this subarray of non-zero
        // integers form totalSubarrays
        totalSubarrays = totalSubarrays - ((count * (count + 1)) / 2);
 
        // Return totalSubarrays as answer
        return totalSubarrays;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 1, 3, 0, 0, 4 };
        int N = arr.Length;
        int K = 1;
 
        // Function Call
        Console.Write(CountSubarrays(N, arr, K));
    }
}


Javascript




//Javascript Implementation
 
// Function to count number of subarrays
// containing atleast 1 negative integer
function countSubarrays(N, arr, K) {
// Variable for total possible number
// of subarrays for given
// array of size number
  let totalSubarrays = (N * (N + 1)) / 2;
// Count variable to find length of
// each subarray containing only
// non-zero integers
  let count = 0;
  let zeroes = 0;
 
  for (let i = 0; i < N; i++) {
    if (arr[i] === 0) {
      zeroes++;
    }
 
    if (zeroes < K) {
      continue;
    }
     // If current element is not zero,
    // increment value of count
    if (arr[i] !== 0) {
      count++;
    } else {
      totalSubarrays = totalSubarrays - (count * (count + 1)) / 2;
      count = 0;
    }
  }
 
  totalSubarrays = totalSubarrays - (count * (count + 1)) / 2;
  return totalSubarrays;
}
 
// Driver code
const arr = [1, 3, 0, 0, 4];
const N = arr.length;
const K = 1;
 
// Function Call
console.log(countSubarrays(N, arr, K));
//This code is contributed by Aditi Tyagi


Output

14




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads