Question
Balanced Binary Subarrays
You are given a binary array of size KaTeX can only parse string typed expression, where each element is either KaTeX can only parse string typed expression or KaTeX can only parse string typed expression. Your task is to count the number of subarrays that contain an equal number of KaTeX can only parse string typed expressions and KaTeX can only parse string typed expressions.
Input
The first line contains a single integer KaTeX can only parse string typed expression — the size of the array.
The second line contains KaTeX can only parse string typed expression integers, representing the elements of the array — KaTeX can only parse string typed expression, each being either KaTeX can only parse string typed expression or KaTeX can only parse string typed expression.
Output
Print a single integer — the number of subarrays that have equal occurrences of KaTeX can only parse string typed expression and KaTeX can only parse string typed expression.
Example
Input
6
0 1 0 0 1 1

Output
6

Explanation
Valid subarrays with equal number of KaTeX can only parse string typed expression and KaTeX can only parse string typed expression:
(0, 1) → [0, 1]
(1, 2) → [1, 0]
(1, 4) → [1, 0, 0, 1]
(2, 5) → [0, 0, 1, 1]
(3, 4) → [0, 1]
(0, 5) → [0, 1, 0, 0, 1, 1]

Total = 6 subarrays.

Online