
The first line of input contains a single integer T, representing the number of test cases.
The first line of each test case contains ‘N’, denoting the number of elements in the array.
The second line of each test case contains the array elements.
The first and only line of each test case in the output contains 1 if the array satisfies the conditions mentioned.
If the array returned is the rearranged array following all the conditions, then it will print 1 else it will print 0.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^4
0 <= array[i] <= 10^5
Time limit: 1 sec
We will sort the array. Then we take an auxiliary array and fill it with the elements of the sorted array starting from both the ends in an alternate order.
The steps are as follows:
We will traverse the array and check if every second element is greater than the left and right elements. If the present element is less than its left element, then we swap the elements. If the current element is less than its right element, then we swap the elements. We continue this for every alternate element. Finally, we return the rearranged array as our final answer.
The steps are as follows: