Make all array elements even by replacing any pair of array elements with their sum | Java program for the above approach ; Function to find the minimum number of replacements required to make all array elements even ; Stores the count of odd elements ; Traverse the array ; Increase count of odd elements ; Store number of replacements required ; Two extra moves will be required to make the last odd element even ; Print the minimum replacements ; Driver Code ; Function call
import java . util . * ; class GFG { static void minMoves ( int arr [ ] , int N ) { int odd_element_cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != 0 ) { odd_element_cnt ++ ; } } int moves = ( odd_element_cnt ) / 2 ; if ( odd_element_cnt % 2 != 0 ) moves += 2 ; System . out . print ( moves ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 3 , 7 , 20 } ; int N = arr . length ; minMoves ( arr , N ) ; } }