Comments
Spoiler
Spoiler

Sort the permutation in descending order and shift the smallest element (i.e 1) one place at a time from right to left. For example if n=4:

4->3->2->1, 4->3->1->2, 4->1->3->2, 1->4->3->2

Weak test case in problem 1529-B. If we take array a as {0,1} answer should be 2.My code is giving output 1 but it still got accepted.