Lexicographically smallest permutation of size A having B integers exceeding all preceding integers | C ++ 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 ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getSmallestArray ( int A , int B ) { vector < int > arr ( A ) ; for ( int i = 0 ; i < A ; i ++ ) arr [ i ] = i + 1 ; vector < int > ans ( A ) ; for ( int i = 0 ; i < B - 1 ; i ++ ) ans [ i ] = arr [ i ] ; ans [ B - 1 ] = arr [ A - 1 ] ; for ( int i = B ; i < A ; i ++ ) ans [ i ] = arr [ i - 1 ] ; return ans ; } int main ( ) { int A = 3 ; int B = 2 ; vector < int > ans = getSmallestArray ( A , B ) ; for ( auto i : ans ) cout << i << " ▁ " ; }