Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

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

```
1 <= ‘T’ <= 10^5
1 <= ‘N’ <= 10^5
0 <= ‘arr[i]’ <= 2
Sum of ‘N’ for all testcases <= 10^5
Time Limit: 1 sec
```

```
2
4
1 2 0 1
3
1 0 1
```

```
0 1 1 2
0 1 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.
```

```
2
4
0 1 1 2
3
2 1 1
```

```
0 1 1 2
1 1 2
```