Subarray with Equal Occurrences Problem Statement

You are provided with an array/list ARR of length N containing only 0s and 1s. Your goal is to determine the number of non-empty subarrays where the number of 0s is equal to the number of 1s.

Input:

The first line contains an integer 'T' indicating the number of test cases. Each test case consists of: 
- The first line contains a positive integer 'N', the length of the array/list.
- The second line contains 'N' space-separated integers representing the elements of the array/list.

Output:

For each test case, output a single line with the count of subarrays in which the number of 0s and 1s are equal. Provide the output for each test case on a new line.

Example:

Input:
2
4
0 1 0 1
5
1 0 0 1 1
Output:
4
2

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5 * 10^3
  • 0 <= ARR[i] <= 1
  • Time limit: 1 sec

Note:

There is no need to print the expected output; it is handled automatically. Focus on implementing the solution function.
AnswerBot
4mo

Count the number of subarrays where the number of 0s is equal to the number of 1s in a given array of 0s and 1s.

  • Iterate through the array and keep track of the count of 0s and 1s encountered so far.

  • Us...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Amdocs

Q. What are the key concepts of Object-Oriented Programming (OOP)?
Q. What is the internal working mechanism of a HashMap?
Q. What is SDLC and what are its types?
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