Subarray Challenge: Largest Equal 0s and 1s

Determine the length of the largest subarray within a given array of 0s and 1s, such that the subarray contains an equal number of 0s and 1s.

Input:

Input begins with a single integer T, representing the number of test cases. For each test case, the input includes:

  • An integer N denoting the length of the array.
  • A line with N space-separated integers, each being either 0 or 1, representing the elements of the array.

Output:

For each test case, output a single integer, the length of the largest subarray where the number of 0s equals the number of 1s.

Example:

Input:
T = 1
N = 5
Array = [0, 0, 1, 0, 1]
Output:
4
Explanation:

The subarray [0, 1, 0, 1] contains 2 zeros and 2 ones, and is the longest such subarray with equal numbers.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • Array elements are either 0 or 1.
  • Time Limit: 1 second

Note:

No need to manually print the output; it's handled within the function.

AnswerBot
4mo

Find the length of the largest subarray with equal number of 0s and 1s in a given array.

  • Iterate through the array and maintain a count of 0s and 1s encountered so far.

  • Store the count difference in a h...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Media.net Software Services (India)

Q. Given a sequence of positive numbers, make it a non-decreasing sequence. Operati...read more
Q. Interleaving Two Strings Problem Statement You are given three strings 'A', 'B',...read more
Q. Boxes of Power Problem Statement Given a set of boxes represented by an array ga...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits