-
Notifications
You must be signed in to change notification settings - Fork 0
/
even_array.java
56 lines (55 loc) · 1.65 KB
/
even_array.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
//1367B Even Array
import java.util.Scanner;
public class even_array {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] a=new int[n];
int even=0,odd=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]%2==0)
even++;
else
odd++;
}
if(even>odd){
int count=0;
for(int i=0;i<n;i++){
if(a[i]%2!=0){
for(int j=i+1;j<n;j++){
if(a[j]%2==0){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
count++;
break;
}
}
}
}
System.out.println(count);
}
else{
int count=0;
for(int i=0;i<n;i++){
if(a[i]%2==0){
for(int j=i+1;j<n;j++){
if(a[j]%2!=0){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
count++;
break;
}
}
}
}
System.out.println(count);
}
}
sc.close();
}
}