Permutation of Array such that products of all adjacent elements are even | Java program to permutation of array such that product of all adjacent elements is even ; Function to print the required permutation ; push odd elements in ' odd ' and even elements in ' even ' ; Check if it possible to arrange the elements ; Else print the permutation ; Print remaining odds are even . and even elements ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static void printPermutation ( int arr [ ] , int n ) { ArrayList < Integer > odd = new ArrayList < Integer > ( ) ; ArrayList < Integer > even = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . add ( arr [ i ] ) ; else odd . add ( arr [ i ] ) ; } int size_odd = odd . size ( ) ; int size_even = even . size ( ) ; if ( size_odd > size_even + 1 ) System . out . println ( " - 1" ) ; else { int i = 0 ; int j = 0 ; while ( i < size_odd && j < size_even ) { System . out . print ( odd . get ( i ) + " ▁ " ) ; ++ i ; System . out . print ( even . get ( j ) + " ▁ " ) ; ++ j ; } while ( i < size_odd ) { System . out . print ( odd . get ( i ) + " ▁ " ) ; ++ i ; } while ( j < size_even ) { System . out . print ( even . get ( j ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 9 , 8 , 10 , 11 } ; int N = arr . length ; printPermutation ( arr , N ) ; } }