-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxMinOfArray.java
33 lines (30 loc) · 1.18 KB
/
MaxMinOfArray.java
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
package codingProblems;
import java.util.Arrays;
/**
* Java program to find largest and smallest number from an array in Java.
* You cannot use any library method both from Java and third-party library.
*/
public class MaxMinOfArray{
public static void main(String args[]) {
largestAndSmallest(new int[]{-20, 34, 21, -87, 92,
Integer.MAX_VALUE});
largestAndSmallest(new int[]{10, Integer.MIN_VALUE, -2});
largestAndSmallest(new int[]{Integer.MAX_VALUE, 40,
Integer.MAX_VALUE});
largestAndSmallest(new int[]{1, -1, 0});
}
public static void largestAndSmallest(int[] numbers) {
int largest = Integer.MIN_VALUE;
int smallest = Integer.MAX_VALUE;
for (int number : numbers) {
if (number > largest) {
largest = number;
} else if (number < smallest) {
smallest = number;
}
}
System.out.println("Given integer array : " + Arrays.toString(numbers));
System.out.println("Largest number in array is : " + largest);
System.out.println("Smallest number in array is : " + smallest);
}
}