-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveZeroes.java
More file actions
38 lines (38 loc) · 831 Bytes
/
MoveZeroes.java
File metadata and controls
38 lines (38 loc) · 831 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
32
33
34
35
36
37
38
/*Given an integer array nums, move all 0's to the end of it while maintaining
the
relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Sample test case
case=1
input=5
0 1 0 3 12
output=[1, 3, 12, 0, 0]
case=2
input=1
0
output=[0] using two pointers in java*/
import java.util.*;
class MoveZeroes{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
int arr[]=new int[num];
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
int index=0;
for(int i=0;i<num;i++){
if(arr[i]!=0){
arr[index]=arr[i];
index++;
}
}
while(index<num){
arr[index]=0;
index++;
}
for(int i=0;i<num;i++){
System.out.print(arr[i]+" ");
}
}
}