-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number30.java
56 lines (55 loc) · 1.1 KB
/
Number30.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
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*;
public class Number30 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,j;
System.out.println("Enter the size of the array");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter "+n+" numbers");
for(i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
ArrayList<Integer> nm=new ArrayList<>();
ArrayList<Integer> kk=new ArrayList<>();
ArrayList<Integer> nm1=new ArrayList<>();
ArrayList<Integer> kk1=new ArrayList<>();
for(i=0;i<n;i++)
{
if(a[i]>0)
nm1.add(a[i]);
else
kk1.add(a[i]);
}
for(i=0;i<nm1.size();i++)
{
if(a[i]<0)
kk.add(a[i]);
}
for(j=n-kk1.size();j<n;j++)
{
if(a[j]>0)
nm.add(a[j]);
}
for(i=0;i<nm1.size()-nm.size();i++)
{
a[i]=nm1.get(i);
}
for(j=0;j<nm.size();j++)
{
a[i++]=nm.get(j);
}
for(j=0;j<kk.size();j++)
{
a[i++]=kk.get(j);
}
for(j=kk1.size()-kk.size();i<n;i++,j++)
{
a[i]=kk1.get(j);
}
System.out.println("The updated array is");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}