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 ; Traverse the given array ; Flip the sign of current element ; Stores the sum of array elements ; Find the sum of the array ; Update sum ; If sum is equal to 0 ; Update pos ; Reset the current element to its original value ; Reset the value of arr [ i ] ; Driver Code
def smallestIndexArrayElementsFlip ( arr , N ) : NEW_LINE INDENT pos = - 1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE sum = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE DEDENT if ( sum == 0 ) : NEW_LINE INDENT pos = i NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE DEDENT DEDENT return pos NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , - 5 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( smallestIndexArrayElementsFlip ( arr , N ) ) NEW_LINE DEDENT