Find the element whose multiplication with | Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code
function minIndex ( arr , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) { if ( 2 * arr [ i ] == sum ) return ( i + 1 ) ; } return - 1 ; } var arr = [ 1 , 3 , - 5 , 3 , 4 ] ; var n = arr . length ; document . write ( minIndex ( arr , n ) ) ;