Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

Total binary subarrays with with x:y 0's and 1'sratio x:y 
Difference between en1 and en2, changed 18 character(s)
Given a **binary array**, find the total number of subarrays with the ratio of 0's and 1's equal to x:y. ↵

Constraints:↵

_n = arr.length_↵

_2<=n<=1e5_↵

_1<=x,y<=n_↵

**Example:** ↵
**arr = [0,1,0,1]**↵
**x = 1**↵
**y = 1**↵

**Output: 4**↵

Explanation:↵
The four possible subarrays are as follows: ↵

[0 1] 0 1    --> [0,1]↵

0 [1 0] 1    --> [1,0]↵

0 1 [0 1]    --> [0,1]↵

[0 1 0 1]    --> [0,1,0,1]↵

With the given constraints, expecting solution no worse than O(n)↵

P.s: Maybe the problem can be solved using the sliding window technique. But unable to apply it due to ratio constraint.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English anupomkar 2022-07-31 18:53:17 18
en1 English anupomkar 2022-07-31 18:50:06 659 Initial revision (published)