-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveZeroesToEnd.java
44 lines (39 loc) · 962 Bytes
/
MoveZeroesToEnd.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
public class MoveZeroesToEnd {
public static void main(String[] args) {
//int[] arr={3,0,22,0,3,2,1};
//int n=arr.length;
//moveZeros(arr,n);
//for(int num:arr){
// System.out.println(num+" ");
//}
int[] nums = {0, 1, 0, 3, 12};
moveZeros(nums);
for (int num : nums) {
System.out.print(num + " ");
}
}
// public static void moveZeros(int[] arr, int n){
// int j=0;
// for(int i=0;i<n;i++){
// if(arr[i]!=0&&arr[j]==0){
// int temp=arr[i];
// arr[i]=arr[j];
// arr[j]=temp;
// }
// if(arr[j]!=0){
// j++;
// }
// }
// }
public static void moveZeros(int[]arr){
int count=0;
for(int num:arr){
if(num!=0){
arr[count++]=num;
}
}
while(count<arr.length){
arr[count++]=0;
}
}
}