Problem overview
The method minAndMax gets a list of integers and returns the smallest and the largest number together. Java has no built-in pair here, so the lesson declares a record, MinMax(int low, int high), and the method returns one. The smallest goes first.
For [4, -2, 9, 0] the result is MinMax[low=-2, high=9]. For a single number, [7], both fields are 7. For [-5, -1, -8] the result is MinMax[low=-8, high=-1].
Approach
Walk the list once and keep two variables: the smallest and the largest number seen so far. Each number is compared with both. The statement asks for a single loop and no built-in min or max, so the work is done by hand.
Step by step
- Start
lowandhighat the first number,nums.get(0). Starting at 0 would be wrong: for[5, 3, 8]it would report a low of 0, which isn't in the list, and for all-negative lists it would report a high of 0. The first number is always a real candidate. - Loop over the list with a for-each loop,
for (int num : nums). - If
numis smaller thanlow, replacelow. - If
numis larger thanhigh, replacehigh. This is a secondif, not anelse: the first number is both, and each number is tested against both. - After the loop, return
new MinMax(low, high).
Following the loop
For [4, -2, 9, 0], low and high both start at 4. Here is what they hold after each number:
| num | low | high |
|---|---|---|
| 4 | 4 | 4 |
| -2 | -2 | 4 |
| 9 | -2 | 9 |
| 0 | -2 | 9 |
The number 0 changes nothing, because it is neither below -2 nor above 9. The first row shows the loop visiting the starting number itself, which leaves both variables as they are.
The loop reads each number exactly once, so the time grows in step with the length of the list, and the method only uses two extra variables.
The solution in Java
class Solution {
public MinMax minAndMax(List<Integer> nums) {
int low = nums.get(0);
int high = nums.get(0);
for (int num : nums) {
if (num < low) {
low = num;
}
if (num > high) {
high = num;
}
}
return new MinMax(low, high);
}
}
The lesson provides the MinMax record. In the for-each loop, int num unboxes each Integer from the list into a plain int, so < and > compare numbers. Printing the result shows the record's values, for example MinMax[low=-2, high=9].
Edge cases and common mistakes
- Starting at 0. With
[-5, -1, -8]a version that starts both at 0 returnsMinMax[low=-8, high=0], and with[5, 3, 8]it returnsMinMax[low=0, high=8]. Both are wrong. - One number. The loop never changes anything, and both fields stay at that number:
MinMax[low=7, high=7]. - Swapped order. The record's first field is the smallest. Passing
highfirst would fail the tests.
Testing
Try these inputs and compare with the expected results:
[4, -2, 9, 0]givesMinMax[low=-2, high=9].[7]givesMinMax[low=7, high=7].[-5, -1, -8]givesMinMax[low=-8, high=-1].- A list with the smallest number last or the largest number first works the same way, because every number is compared with both variables.
Java also has Collections.min and Collections.max for this job, but they scan the list once each. This exercise asks for a single loop that finds both, which is how you would write it when you want to read the data only once.
