
- 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
Largest subarray with equal number of 0s and 1s in C++
Let's see the steps to complete the program.
- Initialise the array.
- Make all zeroes in the array to -1.
- Have a map an empty map to store the previous indexes.
- Initialise sum to 0, max length to 0 and ending index to -1.
- Write a loop that iterates till n.
- Add current element to sum.
- If the sum is equal to 0.
- Update the max length with i + 1.
- And ending index to i.
- If the sum is present in previous sums map and i - previousIndexes[sum] is greater than max length.
- Update the max length and ending index.
- Else add the sum to the previous indexes map.
- Print the starting index endingIndex - maxLength + 1 and ending index endingIndex.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void findTheSubArray(int arr[], int n) { unordered_map<int, int> previousIndexes; int sum = 0, maxLength = 0, endingIndex = -1; for (int i = 0; i < n; i++) { arr[i] = arr[i] == 0 ? -1 : 1; } for (int i = 0; i < n; i++) { sum += arr[i]; if (sum == 0) { maxLength = i + 1; endingIndex = i; } if (previousIndexes.find(sum) != previousIndexes.end()) { if (maxLength < i - previousIndexes[sum]) { maxLength = i - previousIndexes[sum]; endingIndex = i; } }else { previousIndexes[sum] = i; } } cout << endingIndex - maxLength + 1 << " " << endingIndex << endl; } int main() { int arr[] = { 1, 1, 0, 0, 0, 1, 1, 1, 0 }; findTheSubArray(arr, 9); return 0; }
Output
If you run the above code, then you will get the following result.
1 8
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Count Substrings with equal number of 0s, 1s and 2s in C++
- Check if the binary representation of a number has equal number of 0s and 1s in blocks in Python
- Python - List Initialization with alternate 0s and 1s
- Encoding a number string into a string of 0s and 1s in JavaScript
- Maximize number of 0s by flipping a subarray in C++
- Construct an NFA accepting strings with an even number of 0s or an odd number of 1s
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
- XOR counts of 0s and 1s in binary representation in C++
- Segregate all 0s on right and 1s on left in JavaScript
- JavaScript Program for Counting sets of 1s and 0s in a binary matrix
- C Program to construct DFA accepting odd numbers of 0s and 1s
- Longest subarray with absolute difference equal to some number in JavaScript
- Constructing a string of alternating 1s and 0s of desired length using JavaScript
- MySQL query to count the number of 0s and 1s from a table column and display them in two columns?
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python

Advertisements