human
Find number of subarrays with even sum | Python 3 program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ).
result may be large enough not to fit in int.
.
to keep track of subarrays with even sum starting from index i.
if a [ i ] is odd then all subarrays starting from index i + 1 which was odd becomes even when a [ i ] gets added to it ..
if a [ i ] is even then all subarrays starting from index i + 1 which was even remains even and one extra a [ i ] even subarray gets added to it ..
Driver code"
Solution in Python:
 ... What is a code to find number of subarrays with even sum and the other steps required?
assistant
#Here is code to find number of subarrays with even sum and the other steps required.
 def countEvenSum ( arr , n ) :
	res = 0
	s = 0
	for i in reversed ( range ( n ) ) :
		if arr [ i ] % 2 == 1 :
			s = n - i - 1 - s
		else :
			s = s + 1
		res = res + s
	return res
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 2 , 3 , 4 , 1 ]
	n = len ( arr )
	print ( " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ even " " ▁ sum ▁ is " , countEvenSum ( arr , n ) )