-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzerotoend.java
37 lines (30 loc) · 871 Bytes
/
zerotoend.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
import java.util.Scanner;
public class zerotoend {
public int[] zeroend(int[] arr){
int j=0;
for(int i=0; i<arr.length; i++){
if(arr[i]!=0 && arr[j]==0){
int temp=arr[i];
arr[i] =arr[j];
arr[j] =temp;
}
if(arr[j]!=0){
j++;
}
}
return arr;
}
public static void main(String[] args){
zerotoend re= new zerotoend();
Scanner sc = new Scanner(System.in);
int len =sc.nextInt();
int[] arr = new int[len];
for (int i=0; i<len; i++){
arr[i] =sc.nextInt();
}
int zeroAtend[]= re.zeroend(arr);
for(int i=0; i<zeroAtend.length; i++){
System.out.print(zeroAtend[i] + " ");
}
}
}