Subarray with Equal Occurrences Problem Statement

Given an array ARR consisting of 0s and 1s only, your task is to determine the number of non-empty subarrays where the number of 0s and 1s are equal.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case contains: - The first line with a positive integer 'N', the length of the array/list. - The second line with 'N' space-separated integers, each being either 0 or 1, representing the array/list.

Output:

For each test case, output the count of subarrays where the number of 0s and 1s are equal. Each test case's result should be printed on a new line.

Example:

Input:
2
6
0 1 0 1 0 1
4
1 0 1 0
Output:
9
4

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5 * 103
  • 0 <= ARR[i] <= 1

Note the time limit is 1 second.

AnswerBot
4mo

Count the number of subarrays where the number of 0s and 1s are equal 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.

  • Use a hashmap ...read more

Help your peers!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at Amdocs

Q. What do you know about relational databases?
Q. What is the difference between arrays and ArrayLists in Java?
Q. Given a circular cake and a specified number of straight cuts, what is the maxim...read more
Software Engineer 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