Find the maximum and minimum element in an array

Pattern: Traversal and in-place tricks

easy

Find the smallest and the largest value together in one pass, starting both at the first element.

The problem

You are given an array of between 1 and 100,000 whole numbers. Return its smallest and largest values, as the pair [smallest, largest]. For example, for [1000, 11, 445, 1, 330, 3000] the answer is [1, 3000].

This problem is from Love Babbar's 450 sheet. Its main link points to GeeksforGeeks, which this site has not been able to check automatically. The function name getMinMax, the return format and the limits are this guide's own.

Think first

  • Can you find both values in the same pass?
  • What should both start as?
  • How many comparisons does your method make for n elements?

Hints

    Brute force, and why it fails

    Sort the array and read the first and last elements: O(n log n) when O(n) is enough. Two separate passes (one for the minimum, one for the maximum) are O(n) too, and one combined pass reads the array only once.

    The key insight

    The smallest and the largest are both "best so far" problems, and they can share the same walk through the array.

    Dry run

    10000 
    111 
    4452 
    13 
    3304 
    30005 
    smallest1000
    largest1000

    nums = [1000, 11, 445, 1, 330, 3000]. Start both smallest and largest at the first element, 1000.

    Step 1 of 4

    The plan

    smallest = nums[0]
    largest = nums[0]
    for i from 1 to n - 1:
        if nums[i] < smallest: smallest = nums[i]
        if nums[i] > largest: largest = nums[i]
    return [smallest, largest]
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    Two comparisons per element after the first, and two variables.

    Edge cases

    • One element: it is both the smallest and the largest.
    • All equal: both answers are that value.
    • Negative values: starting at nums[0] avoids any wrong starting guess.
    I've built my logic, show the code

    Java

    class Solution {
        public int[] getMinMax(int[] nums) {
            int smallest = nums[0];
            int largest = nums[0];
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] < smallest) {
                    smallest = nums[i];
                }
                if (nums[i] > largest) {
                    largest = nums[i];
                }
            }
            return new int[]{smallest, largest};
        }
    }

    C++

    class Solution {
    public:
        vector<int> getMinMax(vector<int>& nums) {
            int smallest = nums[0];
            int largest = nums[0];
            for (int i = 1; i < static_cast<int>(nums.size()); i++) {
                if (nums[i] < smallest) {
                    smallest = nums[i];
                }
                if (nums[i] > largest) {
                    largest = nums[i];
                }
            }
            return {smallest, largest};
        }
    };

    Python

    class Solution:
        def getMinMax(self, nums):
            smallest = largest = nums[0]
            for i in range(1, len(nums)):
                if nums[i] < smallest:
                    smallest = nums[i]
                if nums[i] > largest:
                    largest = nums[i]
            return [smallest, largest]

    Common mistakes

    • Starting at 0, which is wrong for all-positive or all-negative arrays.
    • Returning the pair in the wrong order, [largest, smallest].

    Similar problems