-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEven-Array.c
More file actions
56 lines (48 loc) · 1022 Bytes
/
Even-Array.c
File metadata and controls
56 lines (48 loc) · 1022 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
int main(){
int testCases, temp;
scanf("%d", &testCases);
int lengthOfArr;
while(testCases--){
int minMoves = 0;
scanf("%d", &lengthOfArr);
int a[lengthOfArr];
for(int i = 0; i < lengthOfArr; i++){
scanf("%d", &a[i]);
}
for(int i = 0; i < lengthOfArr; i++){
if(i % 2 == 0 && a[i] % 2 != 0){
for(int j = i + 1; j < lengthOfArr; j++){
if(j % 2 != 0 && a[j] % 2 == 0){
temp = a[j];
a[j] = a[i];
a[i] = temp;
minMoves++;
break;
}
}
}
if(i % 2 != 0 && a[i] % 2 == 0){
for(int j = i + 1; j < lengthOfArr; j++){
if(j % 2 != 1 && a[j] % 2 != 0){
temp = a[j];
a[j] = a[i];
a[i] = temp;
minMoves++;
break;
}
}
}
}
for(int i = 0; i < lengthOfArr; i++){
if(a[i] % 2 != i % 2){
minMoves = -1;
break;
}
}
printf("%d\n", minMoves);
}
return 0;
}
//problem-link -> https://codeforces.com/problemset/problem/1367/B
//time-complexity -> O(m * n^2)