Lexicographically smallest permutation of size A having B integers exceeding all preceding integers | Java program for the above approach ; Function to find lexicographically smallest permutation of [ 1 , A ] having B integers greater than all the previous elements ; Initial array ; Resultant array ; Append the first ( B - 1 ) elements to the resultant array ; Append the maximum from the initial array ; Copy the remaining elements ; Print the answer ; Driver Code
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static void getSmallestArray ( int A , int B ) { Vector arr = new Vector ( ) ; for ( int i = 0 ; i < A ; i ++ ) arr . add ( i + 1 ) ; Vector ans = new Vector ( ) ; for ( int i = 0 ; i < B - 1 ; i ++ ) ans . add ( arr . get ( i ) ) ; ans . add ( arr . get ( A - 1 ) ) ; for ( int i = B ; i < A ; i ++ ) ans . add ( arr . get ( i - 1 ) ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int A = 3 ; int B = 2 ; getSmallestArray ( A , B ) ; } }