I found a pattern in this problem that, for any n. for example for n=7 and k=37. There are 2^(n-1) possible permutations with max value. Here, the nearest power of 2 equal to greater than 37 is 64. so, here lexicographically first 32 permutations 1st digit is 1. then for the next 16, 1st digit is 2 and for next 8, it is 3 and it goes on until 7. note that here the pattern is 32,16,8,4,2,1,1. 1 occurs two times. Here for 37, the first place is of 2 as 37<=32+16. Next 3 will occur 16/2 times. Here for k=37 we got the sequence 2,3..... as for 2nd place in the sequence, 3 occurs 8 times, it falls within 37, for the 3rd place in the sequence 4 occurs 4 times which is not satisfied, 5 occurs 2 times, which is satisfied . The sequence is 2,3,5..... so for the next place it goes on by doing half of the occurence of the previous element, when we find n as an element in the sequence, we can print all the absent elements of the permuation by reversing. Below are the permutations for n=7 and k=31 to k=43.
1 6 7 5 4 3 2
1 7 6 5 4 3 2
2 3 4 5 6 7 1
2 3 4 5 7 6 1
2 3 4 6 7 5 1
2 3 4 7 6 5 1
2 3 5 6 7 4 1 (37th)
2 3 5 7 6 4 1
2 3 6 7 5 4 1
2 3 7 6 5 4 1
2 4 5 6 7 3 1
2 4 5 7 6 3 1
2 4 6 7 5 3 1
2 4 7 6 5 3 1
2 5 6 7 4 3 1
2 5 7 6 4 3 1
2 6 7 5 4 3 1
2 7 6 5 4 3 1
3 4 5 6 7 2 1
3 4 5 7 6 2 1
3 4 6 7 5 2 1
3 4 7 6 5 2 1
Now, my question is how this is related to the editorial. In the editorial he simply took the binary representation and traversed in reverse and pushed in two different vectors and reversed the second vector. I couldnt relate this two approaches. If anyone can help, that would be great. My approach seemed correct but it was harder for me to implement in the contest.