human
Count subarrays having sum modulo K same as the length of the subarray | Function that counts the subarrays having sum modulo k equal to the length of subarray.
Stores the count of subarrays.
Stores prefix sum of the array.
Calculate prefix sum array.
Generate all the subarrays.
Check if this subarray is a valid subarray or not.
Total count of subarrays.
Given arr [ ].
Size of the array.
Given K.
Function call"
How can the above be solved in Python?
assistant
#Here is code to count subarrays having sum modulo K same as the length of the subarray and the other steps required.
 def countSubarrays ( a , n , k ) :
	ans = 0
	pref = [ ]
	pref . append ( 0 )
	for i in range ( n ) :
		pref . append ( ( a [ i ] + pref [ i ] ) % k )
	for i in range ( 1 , n + 1 , 1 ) :
		for j in range ( i , n + 1 , 1 ) :
			if ( ( pref [ j ] - pref [ i - 1 ] + k ) % k == j - i + 1 ) :
				ans += 1
	print ( ans , end = ' ▁ ' )
arr = [ 2 , 3 , 5 , 3 , 1 , 5 ]
N = len ( arr )
K = 4
countSubarrays ( arr , N , K )