Place first N natural numbers at indices not equal to their values in an array | Java 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
import java . util . * ; class GFG { static void generatepermutation ( int N ) { Vector < Integer > answer = new Vector < Integer > ( ) ; answer . add ( N ) ; for ( int i = 1 ; i < N ; i ++ ) { answer . add ( i ) ; } for ( int i : answer ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 4 ; generatepermutation ( N ) ; } }