
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
We are given an array arr[] containing 0’s and 1’s only. The goal is to count all subarrays of arr[] such that each subarray contains only 0’s or only 1’s not both. If the array is [1,0,0] .Subarrays will be for 0’s only [0], [0], [0,0] and for 1’s only [1].
Let us understand with examples.
Input − arr[] = { 0, 0, 1, 1, 1, 0 };
Output − Subarrays with only 0's are : 4 Subarrays with only 1's are : 6
Explanation − Subaarays will be −
For all 0’s: [0], [0], [0], [0,0]. Total 4 ( arr[0], arr[1], arr[5], arr[0-1] ) For all 1’s: [1], [1], [1], [1,1], [1,1], [1,1,1]. Total 6 ( arr[2], arr[2], arr[4], arr[2-3], arr[3-4], arr[2-4] )
Input − arr[] = { 1,0,1,0 };
Output − Subarrays with only 0's are : 2 Subarrays with only 1's are : 2
Explanation − Subaarays will be −
For all 0’s: [0], [0]. Total 2 ( arr[1], arr[3] ) For all 1’s: [1], [1]. Total 2 ( arr[0], arr[2] )
The approach used in the below program is as follows
We will traverse the array twice for separately counting subarrays with 0’s and 1’s only. Take two counters count_0 and count_1 to store the count of consecutive 0’s and 1’s in the array. For each such count, possible subarrays would be count_0*(count_0+1)/2 and similarly for count_1.
Add this to total_0 count until the end of the array is reached. Do a similar process for 1’s.
Take an array arr[] of numbers.
Function sub_zero_one(int arr[], int size) takes the array and returns the count of subarrays with only 0’s and the count of subarrays with only 1’s.
Take the initial counts as temp_0 and temp_1 for subarrays.
Take the temporary consecutive counts of 0’s and 1’s as count_0 and count_1.
We will traverse the array using for loop from i=0 to I <size.
If the current element is 0 increment count_0.
If not, calculate all possible subarrays with count_0 number of 0’s as temp_one_0=count*(count+1)/2.
Add this to the previous temp_0 for subarrays with all 0’s found so far.
Do a similar process using for loop for 1’s with variables as count_1, temp_one_1 and temp_1.
At the end of both traversals, temp_0 and temp_1 will have respective counts of all subarrays within arr that have all 0’s and all 1’s.
Example
#include <bits/stdc++.h> using namespace std; void sub_zero_one(int arr[], int size){ int count_1 = 0; int count_0 = 0; int temp_1 = 0; int temp_0 = 0; for (int i = 0; i < size; i++){ if (arr[i] == 1){ count_1++; } else{ int temp_one_1 = (count_1) * (count_1 + 1) / 2; temp_1 = temp_1 + temp_one_1; count_1 = 0; } } for (int i = 0; i < size; i++){ if (arr[i] == 0) { count_0++; } else{ int temp_one_0 = (count_0) * (count_0 + 1) / 2; temp_0 = temp_0 + temp_one_0; count_0 = 0; } } if (count_1){ int temp_one_1 = (count_1) * (count_1 + 1) / 2; temp_1 = temp_1 + temp_one_1; } if (count_0){ int temp_one_0 = (count_0) * (count_0 + 1) / 2; temp_0 = temp_0 + temp_one_0; } cout<<"Subarrays with only 0's are : "<<temp_0; cout<<"\nSubarrays with only 1's are : "<<temp_1; } int main(){ int arr[] = { 0, 0, 0, 1, 1, 0, 1}; int size = sizeof(arr) / sizeof(arr[0]); sub_zero_one(arr, size); return 0; }
Output
If we run the above code it will generate the following output −
Subarrays with only 0's are : 7 Subarrays with only 1's are : 4
- Related Articles
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Count 1’s in a sorted binary array in C++
- Find the number of integers from 1 to n which contains digits 0’s and 1’s only in C++
- Javascript Program to Count 1’s in a sorted binary array
- 58000000 × 0 = 0 Why It's 0 Only
- Segregate 0’s and 1’s in an array list using Python?
- Largest number with binary representation is m 1’s and m-1 0’s in C++
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Given a sorted array of 0’s and 1’s, find the transition point of the array in C++
- 1’s and 2’s complement of a Binary Number?
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
