Help identify the issue in code for MEDIANT

Submitted the following code during the contest for problem https://www.codechef.com/START157B/problems/MEDIANT:

https://www.codechef.com/viewsolution/1100970316

It is showing wrong answer for the below input:
1
50
44 2 86 52 95 5 15 37 5 6 11 34 88 41 27 30 11 46 58 4 37 22 75 88 66 4 77 70 54 61 19 85 50 5 88 70 39 75 95 85 4 8 35 63 4 80 19 62 16 91

The array after performing the operations given by my code is: 2 95 95. Is the code accepted only when array size is reduced to 2 (i.e. K = n-2 in the output)? I am checking the array to be sorted instead of performing all n-2 operations to reduce the size to 2. Please help me identify the issue here?