In Java, we can find the minimum and maximum elements in a list using different approaches. The minimum element is the smallest value in the list, while the maximum element is the largest value. Java provides built-in methods such as Collections.min() and Collections.max().
- A single traversal can find both minimum and maximum in O(n) time.
- The list must contain comparable elements when using natural ordering.
Example:
Input:
list = [10, 4, 3, 2, 1, 20]
Output:min = 1, max = 20Input:
list = [10, 400, 3, 2, 1, -1]
Output:min = -1, max = 400
Different Approaches to Find Min and Max in a List
1. Using Sorting
We first sort the list in ascending order. After sorting, the first element is the minimum and the last element is 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));
}
}
Output
Min: 11 Max: 44
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.
2. Using Collections.min() and Collections.max()
Java provides the Collections.min() and Collections.max() methods to directly find the minimum and maximum elements from a list.
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));
}
}
Output
Min: 11 Max: 44
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.
3. Using Linear Traversal
In this approach, we traverse the list once and maintain two variables, min and max. Whenever a smaller or larger element is found, we update the corresponding variable.
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.