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.

Be the first one to answer
Add answer anonymously...
Housing.com Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter