C Exercises: Find the largest subarray with equal number of 0s and 1s
62. Largest Subarray with Equal 0s and 1s
Write a program in C to find the largest subarray with an equal number of 0s and 1s.
Expected Output :
The given array is : 0 1 0 0 1 1 0 1 1 1
Subarray found from the index 0 to 7
To find the largest subarray with an equal number of 0s and 1s, the program converts 0s to -1s and then uses a hashmap to track the cumulative sums. By identifying subarrays where the cumulative sum is the same at different indices, the program can determine the largest subarray with equal 0s and 1s. This approach efficiently finds the required subarray by leveraging the properties of cumulative sums.
Visual Presentation:

Sample Solution:
C Code:
Sample Output:
The given array is : 0 1 0 0 1 1 0 1 1 1 Subarray found from the index 0 to 7
For more Practice: Solve these Related Problems:
- Write a C program to find the longest subarray with an equal number of 0s and 1s using a hash map approach.
- Write a C program to detect the subarray with equal 0s and 1s by converting 0s to -1s and applying Kadane’s algorithm.
- Write a C program to print all subarrays with equal numbers of 0s and 1s along with their indices.
- Write a C program to find the maximum length subarray with equal 0s and 1s using prefix sums.
Go to:
PREV : Maximum Product Subarray.
NEXT : Replace with Greatest Element on Right.
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.