Given a rotated sorted array of size ‘N’ and let the elements of the given array be a1,a2,......,an . You need to sort the given array in increasing order.
For Example - Consider ‘N’ = 4 and the given array is [2,3,4,1] then the output should be [1,2,3,4].
A rotated sorted array is a sorted array in which each element is shifted ‘x’ (‘x’ >= 0 and ‘x’ <= ’N’) times to its right and the rightmost element is shifted to the beginning of the array.
For Example - [3,4,1,2] is a rotated sorted array in which each element is shifted two times to its right.
The first line of input contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first line of each test case contains an integer ‘N’ denoting the size of the given rotated sorted array.
The second line of each test case contains ‘N’ single space-separated integers denoting the elements of the given rotated sorted array.
Output Format :
For each test case, print a single line containing 'N' single space-separated integers representing the elements of the sorted array.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10^4
-10^9 <= a[i] <= 10^9
Where ‘T’ is the total number of test cases, ‘N’ is the size of the array and a[i] is an element of the array.
Time Limit: 1 sec
2
5
6 8 9 2 3
6
7 8 9 1 3 5
2 3 6 8 9
1 3 5 7 8 9
Test case 1 :
The given array [6,8,9,2,3] is rotated around index 3 so the required array after sorting will be [2,3,6,8,9].
Test case 2 :
The given array [7,8,9,1,3,5] is rotated around index 3 so the required array after sorting will be [1,3,5,7,8,9].
2
6
9 1 3 5 7 8
4
4 5 2 3
1 3 5 7 8 9
2 3 4 5
Shift the elements of array one at a time.
Before Iteration
After Iteration
O(N^2), Where ‘N’ is the number of elements in the given array.
In the worst case, each shift operation in the array will require nearly O(N) time and the maximum number of times this shifting needs to be done will be ‘N’.
O(1)
Changes will be made in the given array itself. Hence, no extra space is used.