You have been given an array/list 'arr' consisting of 'n' elements.
Each element in the array is either 0, 1 or 2.
Sort this array/list in increasing order.
Do not make a new array/list. Make changes in the given array/list.
Input: 'arr' = [2, 2, 2, 2, 0, 0, 1, 0]
Output: Final 'arr' = [0, 0, 0, 1, 2, 2, 2, 2]
Explanation: The array is sorted in increasing order.
The first line contains a positive integer ‘n’, which represents the length of the array/list.
The second line of each test case contains ‘n’ single space-separated integers representing the elements of the array/list.
The output will print ‘n’ single space-separated integers of the sorted array/list.
You do not need to print anything; it has already been taken care of. Just implement the given function.
8
2 2 2 2 0 0 1 0
0 0 0 1 2 2 2 2
The initial array 'arr' is [2, 2, 2, 2, 0, 0, 1, 0].
After sorting the array in increasing order, 'arr' is equal to:
[0, 0, 0, 1, 2, 2, 2, 2]
5
1 1 1 1 1
1 1 1 1 1
The expected time complexity is O(n).
1 <= 'n' <= 10 ^ 4
0 <= 'arr[i]' <= 2
Time limit: 1 second
Try to sort the array.
Simply, we will sort the array.
O(n * log(n)), where ‘n’ is the length of the array/list.
Since we are using the inbuilt sort function, the time complexity is O(n * log(n)).
O(1), i.e. constant space complexity.
Since we are not using any extra space. Hence, the space complexity is constant.