-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTask2.java
More file actions
28 lines (24 loc) · 829 Bytes
/
Task2.java
File metadata and controls
28 lines (24 loc) · 829 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
public class Task2 {
public static void findTwoSmallest(int[] numbers) {
if (numbers.length < 2) {
System.out.println("Array must have at least two elements");
return;
}
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int num : numbers) {
if (num < smallest) {
secondSmallest = smallest;
smallest = num;
} else if (num < secondSmallest && num != smallest) {
secondSmallest = num;
}
}
System.out.println("Smallest: " + smallest);
System.out.println("Second smallest: " + secondSmallest);
}
public static void main(String[] args) {
int[] arr = {13, 4, 7, 2, 20, 5};
findTwoSmallest(arr);
}
}