-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove_Zeroes.java
More file actions
48 lines (42 loc) · 1.03 KB
/
Move_Zeroes.java
File metadata and controls
48 lines (42 loc) · 1.03 KB
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
import java.util.*;
public class Main
{
static void swap(int []arr,int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
static void Move_all_Zeros(int []arr,int len){
int i=0;
int j=-1;
while (i < len){
if (arr[i] == 0){
j = i;
break;
}
i++;
}
if (j == -1) {
return;
}
for (i = j; i < len; i++){
if (arr[i] != 0){
swap(arr, i, j);
j++;
}
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of elements:");
int len = input.nextInt();
int []arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = input.nextInt();
}
Move_all_Zeros(arr, len);
for(int i = 0; i < len; i++){
System.out.print(arr[i] + " ");
}
}
}