human
Count number of ways to divide an array into two halves with same sum | Function to count the number of ways to divide an array into two halves with same sum.
if length of array is 1 answer will be 0 as we have to split it into two non - empty halves.
variables to store total sum , current sum and count.
finding total sum.
checking if sum equals total_sum / 2.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count number of ways to divide an array into two halves with same sum and the other steps required.
 def cntWays ( arr , n ) :
	if ( n == 1 ) :
		return 0 ;
	tot_sum = 0 ; sum = 0 ; ans = 0 ;
	for i in range ( 0 , n ) :
		tot_sum += arr [ i ] ;
	for i in range ( 0 , n - 1 ) :
		sum += arr [ i ] ;
		if ( sum == tot_sum / 2 ) :
			ans += 1 ;
	return ans ;
arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ;
n = len ( arr ) ;
print ( cntWays ( arr , n ) ) ;