-
Notifications
You must be signed in to change notification settings - Fork 0
/
Odd_Even.java
64 lines (64 loc) · 968 Bytes
/
Odd_Even.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
57
58
59
60
61
62
63
64
package rank;
import java.util.*;
public class Odd_Even
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
int i=0;
while(i<n)
{
a[i]=sc.nextInt();
i++;
}
for(int j=0;j<n;j++)
{
if(j%2==0){
if(a[j]%2==0)
continue;
else
{
for(int k=j+1;k<n;k++)
{
int tmp=0;
if(a[k]%2==0)
{
tmp=a[j];
a[j]=a[k];
a[k]=tmp;
break;
}
}
}}
else
{
if(a[j]%2!=0)
continue;
else
{
for(int k=j+1;k<n;k++)
{
int tmp=0;
if(a[k]%2!=0)
{
tmp=a[j];
a[j]=a[k];
a[k]=tmp;
break;
}
}
}
}
}
i=0;
while(i<n)
{
System.out.print(a[i]+" ");
i++;
}
}
}/*
Input : arr[] = {10, 9, 7, 18, 13, 19, 4, 20, 21, 14}
Output : 10 9 18 7 20 19 4 13 14 21*/