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
1d

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!
Add answer anonymously...
Amdocs Software Developer 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