188k views
2 votes
The first line of input contains an integer n representating the size of the sequence the second line of input contains n space - separated integers as a1,a2......an representing array elements output print the minimum number of moves to make all’ array no odd

1 Answer

3 votes
Here's a Java code that solves the problem:

```
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = input.nextInt();
}
int oddCount = 0;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
oddCount++;
}
}
int evenCount = n - oddCount;
if (oddCount == 0 || evenCount == 0) {
System.out.println(0);
} else {
System.out.println(Math.min(oddCount, evenCount));
}
}
}
```

The program reads in the size of the sequence and the array elements from the user, and then counts the number of odd and even elements in the array. If the array contains only odd or even elements, then no moves are needed, and the program prints 0. Otherwise, the program prints the minimum number of moves needed to make all the array elements even. This is equal to the minimum of the number of odd elements and the number of even elements in the array.
User Panos Boc
by
8.4k points