Computer Science, asked by SaiPradyumnan4043, 1 year ago

You are given a array containing only 0s and 1s . You have to tell the number of subarrays which has equal number of 0s and 1s.

Answers

Answered by Anonymous
0

Explanation:

are given a array containing only 0s and 1s . You have to tell the number of subarrays which has equal number

Answered by aakashkundanapally
0

Answer:

def countSubarrWithEqualZeroAndOne(arr, n):  

   mp = dict()  

   Sum = 0

   count = 0

   for i in range(n):  

       if (arr[i] == 0):  

           arr[i] = -1

       Sum += arr[i]  

       if (Sum == 0):  

           count+=1

       if (Sum in mp.keys()):  

           count += mp[Sum]  

       mp[Sum] = mp.get(Sum, 0) + 1

   return count

for i in range(int(input())):

   n=int(input())

   arr =list(map(int,input().strip().split()))[:n]  

   print(countSubarrWithEqualZeroAndOne(arr, n))

Explanation:

Similar questions
Math, 1 year ago