Smallest indexed array element required to be flipped to make sum of array equal to 0 | Java program for above approach ; 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 function
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int smallestIndexArrayElementsFlip ( int arr [ ] , int N ) { int pos = - 1 ; int ArrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ArrSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( 2 * arr [ i ] == ArrSum ) { pos = i ; break ; } } return pos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , - 5 , 3 , 4 } ; int N = arr . length ; System . out . println ( smallestIndexArrayElementsFlip ( arr , N ) ) ; } }