-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortNumbers.java
More file actions
executable file
·49 lines (46 loc) · 1.11 KB
/
SortNumbers.java
File metadata and controls
executable file
·49 lines (46 loc) · 1.11 KB
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
/**
* Answer For
* https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s/0
*/
import java.util.Scanner;
public class SortNumbers {
public static void main(String[] args){
Scanner inpScanner=new Scanner(System.in);
int testCaseCounter=inpScanner.nextInt();
int count=0;
int [][] testArray=new int[testCaseCounter][];
while(count<testCaseCounter){
int arraySize=inpScanner.nextInt();
int []arrayx=new int[arraySize];
int counter=0;
int zeropoint=0;
int onepoint=0;
int twopoint=0;
while(counter<arraySize){
int number=Integer.parseInt(inpScanner.next());
if (number==0){
zeropoint++;
}else if (number==1){
onepoint++;
}else if (number==2){
twopoint++;
}
counter++;
}
for(int i=0;i<arraySize;i++){
if(zeropoint-->0)
arrayx[i]=0;
else if (onepoint-->0){
arrayx[i]=1;
}else if (twopoint-->0){
arrayx[i]=2;
}
}
for (int a=0;a<arrayx.length;a++){
System.out.print(arrayx[a]+" ");
}
System.out.println();
testArray[count++]=arrayx;
}
}
}