Count ways to make sum of odd and even indexed elements equal by removing an array element | Function to count array indices whose removal makes sum of odd and even indexed elements equal ; If size of the array is 1 ; If size of the array is 2 ; Stores sum of even - indexed elements of the given array ; Stores sum of odd - indexed elements of the given array ; Traverse the array ; If i is an even number ; Update sumEven ; If i is an odd number ; Update sumOdd ; Stores sum of even - indexed array elements till i - th index ; Stores sum of odd - indexed array elements till i - th index ; Stores count of indices whose removal makes sum of odd and even indexed elements equal ; Stores sum of even - indexed elements after removing the i - th element ; Stores sum of odd - indexed elements after removing the i - th element ; Traverse the array ; If i is an odd number ; Update currOdd ; Update newEvenSum ; Update newOddSum ; If i is an even number ; Update currEven ; Update newOddSum ; Update newEvenSum ; If newEvenSum is equal to newOddSum ; Increase the count ; If sum of even - indexed and odd - indexed elements is equal by removing the first element ; Increase the count ; If length of the array is an odd number ; If sum of even - indexed and odd - indexed elements is equal by removing the last element ; Increase the count ; If length of the array is an even number ; If sum of even - indexed and odd - indexed elements is equal by removing the last element ; Increase the count ; Driver Code
def cntIndexesToMakeBalance ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT sumEven = 0 NEW_LINE sumOdd = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT sumEven += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT sumOdd += arr [ i ] NEW_LINE DEDENT DEDENT currOdd = 0 NEW_LINE currEven = arr [ 0 ] NEW_LINE res = 0 NEW_LINE newEvenSum = 0 NEW_LINE newOddSum = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT currOdd += arr [ i ] NEW_LINE newEvenSum = ( currEven + sumOdd - currOdd ) NEW_LINE newOddSum = ( currOdd + sumEven - currEven - arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT currEven += arr [ i ] NEW_LINE newOddSum = ( currOdd + sumEven - currEven ) NEW_LINE newEvenSum = ( currEven + sumOdd - currOdd - arr [ i ] ) NEW_LINE DEDENT if ( newEvenSum == newOddSum ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT if ( sumOdd == sumEven - arr [ 0 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT if ( sumOdd == sumEven - arr [ n - 1 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( sumEven == sumOdd - arr [ n - 1 ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( cntIndexesToMakeBalance ( arr , n ) ) NEW_LINE DEDENT