KatInterview

628. Maximum Product of Three Numbers

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method Explanation

To find the maximum product of three numbers in an array, we need to consider the impact of negative numbers. A large product can be formed in two main ways:

Solution

Maximum Product of Three Numbers in Python

class Solution:
    def maximumProduct(self, nums: List[int]) -> int:
        # Initialize variables to track the two smallest and three largest numbers.
        # We use float('inf') and float('-inf') to ensure the first few numbers are correctly placed.
        min1, min2 = float('inf'), float('inf')
        max1, max2, max3 = float('-inf'), float('-inf'), float('-inf')

        for n in nums:
            # Update the two smallest numbers.
            if n <= min1:
                min2 = min1
                min1 = n
            elif n <= min2:
                min2 = n

            # Update the three largest numbers.
            if n >= max1:
                max3 = max2
                max2 = max1
                max1 = n
            elif n >= max2:
                max3 = max2
                max2 = n
            elif n >= max3:
                max3 = n

        # The maximum product is the maximum of two cases:
        # 1. The product of the three largest numbers (for cases with mostly positive numbers).
        # 2. The product of the two smallest (most negative) numbers and the largest number.
        return max(max1 * max2 * max3, min1 * min2 * max1)

Maximum Product of Three Numbers in Java

class Solution {
    public int maximumProduct(int[] nums) {
        // Initialize variables to track the two smallest and three largest numbers.
        // We use Integer.MAX_VALUE and Integer.MIN_VALUE to ensure the first few numbers are correctly placed.
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;

        for (int n : nums) {
            // Update the two smallest numbers.
            if (n <= min1) {
                min2 = min1;
                min1 = n;
            } else if (n <= min2) {
                min2 = n;
            }

            // Update the three largest numbers.
            if (n >= max1) {
                max3 = max2;
                max2 = max1;
                max1 = n;
            } else if (n >= max2) {
                max3 = max2;
                max2 = n;
            } else if (n >= max3) {
                max3 = n;
            }
        }

        // The maximum product is the maximum of two cases:
        // 1. The product of the three largest numbers (for cases with mostly positive numbers).
        // 2. The product of the two smallest (most negative) numbers and the largest number.
        return Math.max(max1 * max2 * max3, min1 * min2 * max1);
    }
}

Maximum Product of Three Numbers in C++

class Solution {
public:
    int maximumProduct(vector<int>& nums) {
        // Initialize variables to track the two smallest and three largest numbers.
        // We use INT_MAX for mins and INT_MIN for maxs to ensure the first few numbers are correctly placed.
        int min1 = INT_MAX, min2 = INT_MAX;
        int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;

        for (int n : nums) {
            // Update the two smallest numbers.
            if (n <= min1) {
                min2 = min1;
                min1 = n;
            } else if (n <= min2) {
                min2 = n;
            }

            // Update the three largest numbers.
            if (n >= max1) {
                max3 = max2;
                max2 = max1;
                max1 = n;
            } else if (n >= max2) {
                max3 = max2;
                max2 = n;
            } else if (n >= max3) {
                max3 = n;
            }
        }

        // The maximum product is the maximum of two cases:
        // 1. The product of the three largest numbers (for cases with mostly positive numbers).
        // 2. The product of the two smallest (most negative) numbers and the largest number.
        return max(max1 * max2 * max3, min1 * min2 * max1);
    }
};

Maximum Product of Three Numbers in TypeScript

function maximumProduct(nums: number[]): number {
    // Initialize variables to track the two smallest and three largest numbers.
    // We use Infinity and -Infinity to ensure the first few numbers are correctly placed.
    let min1 = Infinity, min2 = Infinity;
    let max1 = -Infinity, max2 = -Infinity, max3 = -Infinity;

    for (const n of nums) {
        // Update the two smallest numbers.
        if (n <= min1) {
            min2 = min1;
            min1 = n;
        } else if (n <= min2) {
            min2 = n;
        }

        // Update the three largest numbers.
        if (n >= max1) {
            max3 = max2;
            max2 = max1;
            max1 = n;
        } else if (n >= max2) {
            max3 = max2;
            max2 = n;
        } else if (n >= max3) {
            max3 = n;
        }
    }

    // The maximum product is the maximum of two cases:
    // 1. The product of the three largest numbers (for cases with mostly positive numbers).
    // 2. The product of the two smallest (most negative) numbers and the largest number.
    return Math.max(max1 * max2 * max3, min1 * min2 * max1);
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.