
Donald W. answered 04/18/22
Senior Software Engineer with over 25 years of industry experience
Here's my implementation:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
int numbers[] = new int[count];
for (int i=0; i<count; i++) {
numbers[i] = sc.nextInt();
}
sc.close();
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
for (int i=0; i<count; i++) {
int num = numbers[i];
if (num < min1) {
min2 = min1;
min1 = num;
} else if (num < min2) {
min2 = num;
}
}
System.out.format("%s %s\n", min1, min2);
}
}
And the output:
> java Main
5 10 5 3 21 2
2 3
>