Update appNew update is available. Click here to update.

Sort 0s, 1s, 2s

profile
Contributed by
Utkarsh Kumar Choubey
Easy
yellow-spark
0/40
2 upvotes
AmazonWells FargoNewgen Software
+3 more

Problem Statement

You are given an array β€˜arr’ consisting only of 0s , 1s, and 2s.

Sort the given array.

For Example:

For β€˜arr’ = {1, 0, 0, 2, 1}.
β€˜Answer’ = {0, 0, 1, 1, 2}.
β€˜Answer’ should contain all 0s first, then all 1s and all 2s in the end.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <=  β€˜T’ <= 10^5
1 <= β€˜N’ <= 10^5
0 <= β€˜arr[i]’ <= 2
Sum of β€˜N’ for all testcases <= 10^5

Time Limit: 1 sec
Sample Input 1:
2
4
1 2 0 1
3
1 0 1
Sample Output 1:
0 1 1 2
0 1 1
Explanation of sample output 1:
For Test 1:
For β€˜arr’ = {1, 2, 0, 1}.
β€˜Answer’ = {0, 1, 1, 2}.
In β€˜Answer all 0 is placed first, then all the 1s and finally 2.

For Test 2:
For β€˜arr’ = {1, 0, 1}.
β€˜Answer’ = {0, 1, 1}.
In β€˜Answer all 0 is placed first, then all the 1s.
Sample Input 2:
2
4
0 1 1 2
3
2 1 1
Sample Output 2:
0 1 1 2
1 1 2
Full screen
Reset Code
Full screen
Autocomplete
copy-code
Console