Rearrange array such that all even | Java Program to implement the above approach ; Function to check if it the array can be rearranged such such that every even indices contains an even element ; Stores the count of even elements ; Traverse array to count even numbers ; If even_no_count exceeds count of even indices ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void checkPossible ( int a [ ] ) { int even_no_count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 == 0 ) even_no_count ++ ; } if ( a . length / 2 > even_no_count ) { System . out . println ( " No " ) ; return ; } System . out . println ( " Yes " ) ; int j = 0 ; for ( int i = 1 ; i < a . length ; i += 2 ) { if ( a [ i ] % 2 == 0 ) continue ; else { while ( j < a . length && a [ j ] % 2 != 0 ) j += 2 ; a [ i ] += a [ j ] ; a [ j ] = a [ i ] - a [ j ] ; a [ i ] -= a [ j ] ; } } for ( int i = 0 ; i < a . length ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; checkPossible ( arr ) ; } }