Rearrange array such that all even | C # 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
using System ; 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 ) { Console . WriteLine ( " No " ) ; return ; } Console . WriteLine ( " 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 ++ ) { Console . Write ( a [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 6 , 7 } ; checkPossible ( arr ) ; } }