Блог пользователя Lamentis

Автор Lamentis, история, 2 года назад, По-английски

This question came in the recent summer internship drive on my campus but I could not solve it

Q)Array contains only 0 and 1. You are given two integers X and Y. Count how many subarrays have (count of 0) to (count of 1) equal to X: Y.

1<=N<=1e5 1<=X,Y<=1e5

Please help. Thanks

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
2 года назад, # |
Rev. 2   Проголосовать: нравится -16 Проголосовать: не нравится

edit: I thought it was two pointers, turns out it wasnt facepalm

»
2 года назад, # |
  Проголосовать: нравится +28 Проголосовать: не нравится

change all 0s to y, and all 1s to -x. Make a prefix sum array of this.

subarray i,j, with i>0 fulfills the condition if and only if pre[j]=pre[i-1]

For i=0, pre[j] needs to be 0

count how many times each prefix appears and do some math

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If the string contains only 0s and 1s, then you could have just googled and found this . I think that string contained 0,1 and 2 because I was also asked the same in Intuit OA. You can find the answer for 0,1,2 here in Kallaseldor's comment