Generate an N | C ++ program for the above approach ; Function to print the permutation of size N with absolute difference of adjacent elements in range [ 2 , 4 ] ; If N is less than 4 ; Check if N is even ; Traverse through odd integers ; Update the value of i ; Traverse through even integers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getPermutation ( int N ) { if ( N <= 3 ) { cout << -1 ; return ; } int i = N ; if ( N % 2 == 0 ) i -- ; while ( i >= 1 ) { cout << i << " ▁ " ; i -= 2 ; } cout << 4 << " ▁ " << 2 << " ▁ " ; i = 6 ; while ( i <= N ) { cout << i << " ▁ " ; i += 2 ; } } int main ( ) { int N = 9 ; getPermutation ( N ) ; return 0 ; }