Find the total number of Subarrays of 0’s
Last Updated :
13 Sep, 2023
Given an array arr[] of length N of 0’s and 1’s, the task is to find the total number of subarrays of 0’s.
Examples:
Input: N = 4, arr[] = {0, 0, 1, 0}
Output: 4
Explanation: Following are the subarrays of length 1: {0}, {0}, {0} – 3 length 2: {0, 0} – 1. Total Subarrays: 3 + 1 = 4
Input: N = 4, arr[] = {0, 0, 0, 0}
Output: 10
Explanation: The following are the subarrays of
- length 1: {0}, {0}, {0}, {0} = 4
- length 2: {0, 0}, {0, 0}, {0, 0} = 3
- length 3: {0, 0, 0}, {0, 0, 0} = 2
- length 4: {0, 0, 0, 0} = 1
Total Subarrays: 4 + 3 + 2 + 1 = 10
Approach: To solve the problem follow the below idea:
The concept is that if there are n consecutive 0s, then there are ((n+1) * (n))/2 total 0 subarrays.
Steps involved in the implementation of code:
- Maintain a variable for the response, initialize it with 0, and another variable for the counter, which keeps track of the number of continuous 0s.
- Start a for loop and traverse through the array.
- Count the number of contiguous zeros.
- Including count*(count+1)/2 in the solution because count*(count+1)/2 subarrays can be produced using the count number of continuous zeros.
- Add count*(count+1)/2 to the answer if the count is more than zero.
- Return the answer.
Below is the code implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
long long no_of_subarrays( int N, int arr[])
{
long long count = 0, answer = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
count++;
}
else {
answer += ((count * (count + 1)) / 2);
count = 0;
}
}
answer += ((count * (count + 1)) / 2);
return answer;
}
int main()
{
int arr[] = { 0, 0, 1, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
long long result = no_of_subarrays(N, arr);
cout << result << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static long no_of_subarrays( int N, int [] arr)
{
long count = 0 , answer = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 0 ) {
count++;
}
else {
answer += ((count * (count + 1 )) / 2 );
count = 0 ;
}
}
answer += ((count * (count + 1 )) / 2 );
return answer;
}
public static void main(String[] args)
{
int [] arr = { 0 , 0 , 1 , 0 };
int N = arr.length;
long result = no_of_subarrays(N, arr);
System.out.println(result);
}
}
|
Python3
def no_of_subarrays(N, arr):
count = 0
answer = 0
for i in range (N):
if arr[i] = = 0 :
count + = 1
else :
answer + = (count * (count + 1 )) / / 2
count = 0
answer + = (count * (count + 1 )) / / 2
return answer
arr = [ 0 , 0 , 1 , 0 ]
N = len (arr)
result = no_of_subarrays(N, arr)
print (result)
|
C#
using System;
public class GFG {
static long no_of_subarrays( int N, int [] arr)
{
long count = 0, answer = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
count++;
}
else {
answer += ((count * (count + 1)) / 2);
count = 0;
}
}
answer += ((count * (count + 1)) / 2);
return answer;
}
static public void Main()
{
int [] arr = { 0, 0, 1, 0 };
int N = arr.Length;
long result = no_of_subarrays(N, arr);
Console.WriteLine(result);
}
}
|
Javascript
function no_of_subarrays(N, arr) {
let count = 0;
let answer = 0;
for (let i = 0; i < N; i++) {
if (arr[i] == 0) {
count++;
}
else {
answer += ((count * (count + 1)) / 2);
count = 0;
}
}
answer += ((count * (count + 1)) / 2);
return answer;
}
const arr = [0, 0, 1, 0];
const N = arr.length;
const result = no_of_subarrays(N, arr);
console.log(result);
|
Time Complexity: O(N).
Auxiliary Space: O(1).
Approach 2: Sliding Window
Implementation :
C++
#include <iostream>
#include <vector>
int GFG(std::vector< int >& arr) {
int count = 1;
int start = 2;
int end = 0;
int n = arr.size();
while (end < n) {
if (arr[end] == 0) {
count++;
}
else {
start = end + 1;
}
end++;
}
return count;
}
int main() {
std::vector< int > arr = {0, 0, 0, 0, 0};
int result = GFG(arr);
std::cout << "Total number of subarrays of 0's: " << result << std::endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static int GFG(List<Integer> arr) {
int count = 1 ;
int start = 2 ;
int end = 0 ;
int n = arr.size();
while (end < n) {
if (arr.get(end) == 0 ) {
count++;
}
else {
start = end + 1 ;
}
end++;
}
return count;
}
public static void main(String[] args) {
List<Integer> arr = new ArrayList<>();
arr.add( 0 );
arr.add( 0 );
arr.add( 0 );
arr.add( 0 );
arr.add( 0 );
int result = GFG(arr);
System.out.println( "Total number of subarrays of 0's: " + result);
}
}
|
Python
def GFG(arr):
count = 1
start = 2
end = 0
n = len (arr)
while end < n:
if arr[end] = = 0 :
count + = 1
else :
start = end + 1
end + = 1
return count
arr = [ 0 , 0 , 0 , 0 , 0 ]
result = GFG(arr)
print ( "Total number of subarrays of 0's:" , result)
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static int GFG(List< int > arr)
{
int count = 1;
int end = 0;
int n = arr.Count;
while (end < n)
{
if (arr[end] == 0)
{
count++;
}
end++;
}
return count;
}
public static void Main( string [] args)
{
List< int > arr = new List< int > { 0, 0, 0, 0, 0 };
int result = GFG(arr);
Console.WriteLine( "Total number of subarrays of 0's: " + result);
}
}
|
Javascript
function GFG(arr) {
let count = 1;
let start = 2;
let end = 0;
const n = arr.length;
while (end < n) {
if (arr[end] === 0) {
count++;
} else {
start = end + 1;
}
end++;
}
return count;
}
function main() {
const arr = [0, 0, 0, 0, 0];
const result = GFG(arr);
console.log(`Total number of subarrays of 0's: ${result}`);
}
main();
|
Output
Total number of subarrays of 0's: 6
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...