Lexicographically smallest permutation of first N natural numbers having K perfect indices | Java program for the above approach ; Function to print the lexicographically smallest permutation with K perfect indices ; Iterator to traverse the array ; Traverse first K array indices ; Traverse remaining indices ; Driver Code
import java . util . * ; class GFG { static void findPerfectIndex ( int N , int K ) { int i = 0 ; for ( ; i < K ; i ++ ) { System . out . print ( ( N - K + 1 ) + i + " ▁ " ) ; } for ( ; i < N ; i ++ ) { System . out . print ( i - K + 1 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 10 , K = 3 ; findPerfectIndex ( N , K ) ; } }