-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1june(III).java
More file actions
31 lines (26 loc) · 764 Bytes
/
Copy path1june(III).java
File metadata and controls
31 lines (26 loc) · 764 Bytes
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
public class Sort0and1 {
public static void main(String[] args) {
// TC = O(n) and SC = O(1)
// algo -
// count the number of zeroes and run a for loop till total number of zeroes
// then fill the indices with zero and rest for 1 we have to run a for loop
// from count to length of the array.
int[] arr = {0,1,0,0,1,1,1,0,0,0,1,0};
int n = arr.length;
int count = 0;
for(int x : arr){
if(x==0){
count+=1;
}
}
for(int i =0; i<count; i++){
arr[i]=0;
}
for(int i = count; i<n;i++){
arr[i]=1;
}
for(int x: arr){
System.out.print(x+",");
}
}
}