PERMSUFF- wrong answer

Can anybody help me with my code for the problem PERMSUFF of August Cook-Off challenge.
Also tell me if i am using the correct logic or not.
I am looking for a number which remains at its position after the shuffling, and once i have the numbers
with me then i sort out the array and check whether the smallest number of that array could be shuffled
using the pair of integers.

If my logic is not correct, then please explain me a better logic.
Link : http://www.codechef.com/viewsolution/4625685

Hey! You are not combining the intervals which overlap with each other. If suppose we have intervals 1 3 and another 2 4 then we can merge these two and have a new interval as 1 4 and we can shuffle all elements in that. You should merge all possible intervals then apply sorting in those intervals and match if you get the original array.

//