-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_0_1_2.py
66 lines (47 loc) · 1.19 KB
/
sort_0_1_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# https://www.codingninjas.com/codestudio/problems/sort-0-1-2_631055?source=youtube&campaign=LoveBabbar_Codestudiovideo1&utm_source=youtube&utm_medium=affiliate&utm_campaign=LoveBabbar_Codestudiovideo1&leftPanelTab=0
from sys import stdin, setrecursionlimit
setrecursionlimit(10 ** 7)
def sort012(arr, n):
# write your code here
# don't return anything
c0,c1,c2 = 0,0,0
final_arr = []
for i in range(n):
if arr[i] == 0 :
c0 += 1
if arr[i] == 1 :
c1 += 1
if arr[i] == 2 :
c2 += 1
j = 0
while(c0 > 0):
arr[j] = 0
j+=1
c0-=1
while(c1 > 0):
arr[j] = 1
j+=1
c1-=1
while(c2 > 0):
arr[j] = 2
j+=1
c2-=1
return(arr)
pass
# taking inpit using fast I/O
def takeInput():
n = int(input().strip())
if n == 0:
return list(), 0
arr = list(map(int, stdin.readline().strip().split(" ")))
return arr, n
def printAnswer(arr, n):
for i in range(n):
print(arr[i], end=" ")
print()
# main
t = int(input().strip())
for i in range(t):
arr, n = takeInput()
sort012(arr, n)
printAnswer(arr, n)