Start learning free

LESSON 261 OF 671 · RECORDS, SORTING & BUILT-INS

Min and Max in Java: One Pass with a Record

Practice Read the full explanation

You get an ArrayList of integers nums. Return its smallest and its largest number together, as a pair.

The smallest number comes first. The function's return type shows how your language holds the pair.

Find both in one loop. Don't use a built-in min or max function.

 

Example 1:

Input: nums = [4, -2, 9, 0]
Output: (-2, 9)
Explanation: The smallest number is -2.
The largest number is 9.

Example 2:

Input: nums = [7]
Output: (7, 7)
Explanation: The only number is both the smallest and the largest.

Example 3:

Input: nums = [-5, -1, -8]
Output: (-8, -1)
Explanation: All the numbers are below 0.
The smallest is -8, and the largest is -1.

 

Constraints:

  • nums has at least one number.
  • Every number is between -106 and 106.
STEP 1 OF 5
    Ctrl + Enter to check

    JAVA · RECORDS, SORTING & BUILT-INS

    Min And Max in Java, explained

    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

    1. Start low and high at 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.
    2. Loop over the list with a for-each loop, for (int num : nums).
    3. If num is smaller than low, replace low.
    4. If num is larger than high, replace high. This is a second if, not an else: the first number is both, and each number is tested against both.
    5. 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:

    numlowhigh
    444
    -2-24
    9-29
    0-29

    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 returns MinMax[low=-8, high=0], and with [5, 3, 8] it returns MinMax[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 high first would fail the tests.

    Testing

    Try these inputs and compare with the expected results:

    • [4, -2, 9, 0] gives MinMax[low=-2, high=9].
    • [7] gives MinMax[low=7, high=7].
    • [-5, -1, -8] gives MinMax[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.

    Keep going with Java500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free