Permutation of Array such that products of all adjacent elements are even | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPermutation ( int arr [ ] , int n ) { vector < int > odd , even ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even . push_back ( arr [ i ] ) ; else odd . push_back ( arr [ i ] ) ; } int size_odd = odd . size ( ) ; int size_even = even . size ( ) ; if ( size_odd > size_even + 1 ) cout << -1 << endl ; else { int i = 0 ; int j = 0 ; while ( i < size_odd && j < size_even ) { cout << odd [ i ] << " ▁ " ; ++ i ; cout << even [ j ] << " ▁ " ; ++ j ; } while ( i < size_odd ) { cout << odd [ i ] << " ▁ " ; ++ i ; } while ( j < size_even ) { cout << even [ j ] << " ▁ " ; } } } int main ( ) { int arr [ ] = { 6 , 7 , 9 , 8 , 10 , 11 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPermutation ( arr , N ) ; return 0 ; }