Make all the elements of array even with given operations | Java program to make all array element even ; Function to count the total number of operations needed to make all array element even ; Traverse the given array ; If an odd element occurs then increment that element and next adjacent element by 1 ; Traverse the array if any odd element occurs then return - 1 ; Returns the count of operations ; Driver code
class GFG { static int countOperations ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { arr [ i ] ++ ; arr [ i + 1 ] ++ ; count += 2 ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) return - 1 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; System . out . print ( countOperations ( arr , n ) ) ; } }