Please help me understand what the error is. Here are the conditions of the problem: Print the value of the smallest positive odd element of the array, and if there are no positive odd elements in the array, print the number 0.
Input data format
In the first line, enter the number of elements in the array. The array elements are entered in the second line.
Output data format
Print the answer to the problem.
Sample Input:
9
-231 -97 13 41 9 -22 46 17 12
Sample Output:
9
Here is my code (it works, but the site says that the task was solved incorrectly):
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner dashka = new Scanner(System.in);
int n = dashka.nextInt();
int[] a = new int[n];
int min = 0;
int y = 0;
for (int i = 0; i < n; i++) {
a[i] = dashka.nextInt();
if (i == y && a[i] > 0) {
min = a[i];
}
else if ((a[i] == 0 || a[i] < 0) && i % 2 != 0) {
y += 2;
}
if (i % 2 == 0 && a[i] > 0) {
if (a[i] < min) {
min = a[i];
}
}
}
System.out.print(min);
}
}
I kind of checked all the input options, and now I’m at a dead end because I can’t find the error.
Дина is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.