-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-4.java
70 lines (57 loc) · 1.1 KB
/
Problem-4.java
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
66
67
68
69
70
/**
* @author AkashGoyal
* @date 24/05/2021
*/
/**
--------------------- Problem----------->> Sort an array of 0s, 1s and 2s
Problem Link :- https://practice.geeksforgeeks.org/problems/sort-an-array-of-0s-1s-and-2s4231/1
*/
//Initial template for Java
import java.io.*;
import java.util.*;
// } Driver Code Ends
//User function template for Java
class Solution
{
public static void sort012(int a[], int n)
{
// code here
int zeros=0;
int ones=0;
int twos=0;
for(int i=0;i<n;i++)
{
if(a[i]==0)
{
zeros++;
}
else if(a[i]==1)
{
ones++;
}
else
{
twos++;
}
}
int i=0;
while(zeros>0)
{
a[i]=0;
i++;
zeros--;
}
while(ones>0)
{
a[i]=1;
i++;
ones--;
}
while(twos>0)
{
a[i]=2;
i++;
twos--;
}
}
}