-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathend_of_array.java
More file actions
42 lines (39 loc) · 858 Bytes
/
end_of_array.java
File metadata and controls
42 lines (39 loc) · 858 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
39
40
41
42
/*
Input -> 7
1 2 0 0 5 0 7
Output -> 1 2 5 7 0 0 0
*/
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
int i,j,temp;
int[] ar=new int[n];
for(i=0;i<n;i++)
{
ar[i]=scan.nextInt();
System.out.print(ar[i]+" ");
}
for(i=0;i<n;i++)
{
if(ar[i]==0)
{
for(j=i;j<n-1;j++)
ar[j]=ar[j+1];
ar[n-1]=0;
}
else if(ar[i+1]==0)
{
for(j=i+1;j<n-1;j++)
ar[j]=ar[j+1];
ar[n-1]=0;
}
}
System.out.println();
for(i=0;i<n;i++)
System.out.print(ar[i]+" ");
}
}