1

Sort An Array of 0s, 1s and 2s

Difficulty: EASY
Avg. time to solve
10 min
Success Rate
90%

Problem Statement
Suggest Edit

You have been given an array/list ARR consisting of ‘N’ elements. Each element in the array is either 0, 1 or 2.

Now, your task is to sort this array/list in increasing order. For example, if ARR = [0, 2, 1, 1], then after sorting ARR must look like [0, 1, 1, 2].

Input Format:
The first line contains an integer ‘T’ denoting the number of test cases. Then each test case follows.

The first line of each test case 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.
Output Format:
For each test case, the only line of output will print ‘N’ single space-separated integers of the sorted array/list.  

Print the output of each test case in a separate line.

Note:

You are not required to print the expected output; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 100
1 <= N <= 5000
0 <= ARR[i] <= 2

Time limit: 1 sec
Sample Input 1:
2
6
0 1 2 0 1 2
8
2 2 2 2 0 0 1 0
Sample output 1:
0 0 1 1 2 2 
0 0 0 1 2 2 2 2
Explanation of Sample output 1:
For the first test case, the sorted array is 0 0 1 1 2 2.

For the second test case, the sorted array is 0 0 0 1 2 2 2 2.
Sample Input 2:
2
4 
0 0 0 0
5
1 1 1 1 1
Sample output 2:
0 0 0 0
1 1 1 1 1
Reset Code
Full screen
copy-code
Console