Smallest indexed array element required to be flipped to make sum of array equal to 0 | Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Stores the sum of the array ; Traverse the given array ; Update ArrSum ; Traverse the given array ; If sum of array elements double the value of the current element ; Update pos ; Driver Code
def smallestIndexArrayElementsFlip ( arr , N ) : NEW_LINE INDENT pos = - 1 NEW_LINE ArrSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT ArrSum += arr [ i ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( 2 * arr [ i ] == ArrSum ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT DEDENT return pos NEW_LINE DEDENT arr = [ 1 , 3 , - 5 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( smallestIndexArrayElementsFlip ( arr , N ) ) NEW_LINE