Code is here
Getting TLE in more than 1 test case
You will get TLE because you are trying to generate all the permutations(n! time), and checking whether the given permuation exists or not will take n^3 time, try to generate only unique permutations and not all of them.
You can try sorting the numbers first, and then while swapping the numbers, try to think of a condition that will generate only unique permutations. Condition will somewhat look like if(j==pos or a[pos]!=a[j])
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.