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
function smallestIndexArrayElementsFlip ( arr , N ) { let pos = - 1 ; let ArrSum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { ArrSum += arr [ i ] ; } for ( let i = 0 ; i < N ; i ++ ) { if ( 2 * arr [ i ] == ArrSum ) { pos = i ; break ; } } return pos ; } let arr = [ 1 , 3 , - 5 , 3 , 4 ] ; let N = arr . length ; document . write ( smallestIndexArrayElementsFlip ( arr , N ) ) ;