
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Maximize number of 0s by flipping a subarray in C++
- Python - List Initialization with alternate 0s and 1s
- XOR counts of 0s and 1s in binary representation in C++
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
- Encoding a number string into a string of 0s and 1s in JavaScript
- C Program to construct DFA accepting odd numbers of 0s and 1s
- Construct an NFA accepting strings with an even number of 0s or an odd number of 1s
- Constructing a string of alternating 1s and 0s of desired length using JavaScript
- Minimum flips to make all 1s in left and 0s in right in C++
- Find the index of first 1 in an infinite sorted array of 0s and 1s in C++
- Segregate all 0s on right and 1s on left in JavaScript
- Longest subarray with absolute difference equal to some number in JavaScript
- Subarray pairs with equal sums in JavaScript
Advertisements