In Java, we can find the smallest (minimum) and largest (maximum) numbers in a list of integers in different ways: by sorting the list, using built-in methods like Collections.min() and Collections.max(), or by checking each number one by one. Each way works, but some are faster and use less memory than others.
Examples
Input : list = [10, 4, 3, 2, 1, 20]
Output : max = 20, min = 1
Input : list = [10, 400, 3, 2, 1, -1]
Output : max = 400, min = -1
1. Using Sorting
This is the least efficient approach. The idea is to sort the list in natural order. After sorting, the first element becomes the minimum and the last element becomes the maximum.
import java.util.*;
public class GFG {
public static Integer findMin(List<Integer> list) {
if (list == null || list.isEmpty())
return Integer.MAX_VALUE;
List<Integer> sortedList = new ArrayList<>(list);
Collections.sort(sortedList);
return sortedList.get(0);
}
public static Integer findMax(List<Integer> list) {
if (list == null || list.isEmpty())
return Integer.MIN_VALUE;
List<Integer> sortedList = new ArrayList<>(list);
Collections.sort(sortedList);
return sortedList.get(sortedList.size() - 1);
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(44);
list.add(11);
list.add(22);
list.add(33);
System.out.println("Min: " + findMin(list));
System.out.println("Max: " + findMax(list));
}
}
Explanation:
- Creates a copy of the original list to avoid modifying it.
- Sorts the copied list in natural ascending order using Collections.sort().
- Retrieves the first element as the minimum value.
- Retrieves the last element as the maximum value.
- Handles empty or null lists by returning Integer.MAX_VALUE for min and Integer.MIN_VALUE for max.
- Time Complexity: O(N log N) and Auxiliary Space: O(N)
2. Using Collections.min() and Collections.max()
Java provides built-in methods Collections.min() and Collections.max() to directly find the minimum and maximum values from a collection.
import java.util.*;
public class GFG {
public static Integer findMin(List<Integer> list) {
if (list == null || list.isEmpty())
return Integer.MAX_VALUE;
return Collections.min(list);
}
public static Integer findMax(List<Integer> list) {
if (list == null || list.isEmpty())
return Integer.MIN_VALUE;
return Collections.max(list);
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(44);
list.add(11);
list.add(22);
list.add(33);
System.out.println("Min: " + findMin(list));
System.out.println("Max: " + findMax(list));
}
}
Explanation:
- Checks if the list is null or empty and returns Integer.MAX_VALUE for min and Integer.MIN_VALUE for max.
- Uses Collections.min(list) to get the smallest element.
- Uses Collections.max(list) to get the largest element.
- Time Complexity: O(N) and Auxiliary Space: O(1)
3. Naive Approach (Linear Traversal)
In this approach, we iterate through the list and keep track of the minimum and maximum values encountered so far.
import java.util.*;
public class GFG {
public static Integer findMin(List<Integer> list) {
Integer min = Integer.MAX_VALUE;
for (Integer i : list) {
if (i < min) {
min = i;
}
}
return min;
}
public static Integer findMax(List<Integer> list) {
Integer max = Integer.MIN_VALUE;
for (Integer i : list) {
if (i > max) {
max = i;
}
}
return max;
}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(44);
list.add(11);
list.add(22);
list.add(33);
System.out.println("Min: " + findMin(list));
System.out.println("Max: " + findMax(list));
}
}
Output
Min: 11 Max: 44
Explanation:
- Initializes min with Integer.MAX_VALUE and max with Integer.MIN_VALUE.
- Loops through each element in the list:
- Updates min if a smaller element is found.
- Updates max if a larger element is found.
- Time Complexity: O(N) and Auxiliary Space: O(1)