TLE in last case of Unlock

ques: https://hack.codingblocks.com/contests/c/547/1556
code: https://ide.codingblocks.com/s/55883

I tried a bit to play with the case of k>n, as that is the reason for this TLE but I couldn’t resolve it.

you logic is wrong, Try a approach in O(N). using hashing.

and why exactly is it wrong?

please can you describe your logic, its difficult to understand you code.

finding the max element from 0 to n, if its not at 0, then swap 0th index value with max, increment k and increment begining index from 0 to 1 and continue till we reach k. If k is greater than n, stop as soon as array is reverse sorted.

Hey,
TLE is occurring because of searching for the maximum element(max() function on line 59) and comparision function(comp() function on line 56)…

Rather than using brute force for comparing and finding max element, use hashing (MAP).