Place first N natural numbers at indices not equal to their values in an array | C ++ program for the above approach ; Function to place first N natural numbers in an array such that none of the values are equal to its indices ; Stores the required array ; Place N at the first position ; Iterate the range [ 1 , N ) ; Append elements to the sequence ; Print the sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generatepermutation ( int N ) { vector < int > answer ; answer . push_back ( N ) ; for ( int i = 1 ; i < N ; i ++ ) { answer . push_back ( i ) ; } for ( int i : answer ) cout << i << " ▁ " ; } int main ( ) { int N = 4 ; generatepermutation ( N ) ; return 0 ; }