628. Maximum Product of Three Numbers - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

Given an integer array nums, find three numbers whose product is maximum and return the maximum product.

Examples

  1. Input: nums = [1,2,3]
    Output: 6
    Explanation: 1 × 2 × 3 = 6
  2. Input: nums = [1,2,3,4]
    Output: 24
    Explanation: 2 × 3 × 4 = 24
  3. Input: nums = [-10,-10,5,2]
    Output: 500
    Explanation: (−10) × (−10) × 5 = 500

Constraints

  • 3 ≤ nums.length ≤ 10⁴
  • -1000 ≤ nums[i] ≤ 1000

Hints

  1. If you sort the array, which three positions could yield the maximum product?
  2. What role do negative numbers play when multiplied in pairs?

Approach 1 Brute Force Triplet Enumeration

Explanation

Try every combination of three distinct indices (i, j, k), compute nums[i] * nums[j] * nums[k], and track the maximum. This guarantees correctness but is too slow for large inputs.

Step‑by‑step Walkthrough

  1. Initialize maxProduct = -∞.

  2. Loop i from 0 to n−3.

  3. Loop j from i+1 to n−2.

  4. Loop k from j+1 to n−1.

  5. Compute prod = nums[i] * nums[j] * nums[k].

  6. Update maxProduct = max(maxProduct, prod).

  7. Return maxProduct.

Visual Example

nums = [−10, 5, 2, −10]
Triplets and products:
(−10, 5, 2) → −100
(−10, 5, −10) → 500  ← maximum
(−10, 2, −10) → 200
(5, 2, −10) → −100
…etc.

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time O(n³)
  • Space O(1)

Approach 2 Sorting Based

Explanation

After sorting, the maximum product is either the product of the three largest numbers or the product of the two smallest (most negative) numbers and the largest number.

Step‑by‑step Walkthrough

  1. Sort nums.
  2. Compute a = nums[n-1] * nums[n-2] * nums[n-3].
  3. Compute b = nums[0] * nums[1] * nums[n-1].
  4. Return max(a, b).

Visual Example

nums = [−10, 2, 5, −10] → sorted [−10, −10, 2, 5]
a = 5 × 2 × (−10) = −100
b = (−10) × (−10) × 5 = 500  ← answer

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time O(n log n) due to sorting
  • Space O(1) (or O(log n) if sorting in-place stack usage counted)

Approach 3 One‑pass Scan (Optimal)

Explanation

Track the three largest and two smallest elements in one pass; then compute the same two candidate products as in sorting.

Step‑by‑step Walkthrough

  1. Initialize max1, max2, max3 = -∞ and min1, min2 = +∞.
  2. For each x in nums:
    • Update max1, max2, max3 appropriately if x is among the top three.
    • Update min1, min2 if x is among the bottom two.
  3. Return max(max1*max2*max3, min1*min2*max1).

Visual Example

nums = [−10, 2, 5, −10]
After scan:
max1=5, max2=2, max3=−10
min1=−10, min2=−10
Compare 5*2*(−10)=−100 vs (−10)*(−10)*5=500 → 500

Python Code

Python3
Python3

. . . .

Java Code

Java
Java

. . . .

Complexity Analysis

  • Time O(n)
  • Space O(1)

Common Mistakes

  • Forgetting that two large negatives can boost the product
  • Only tracking the top three positives
  • Incorrectly updating maxima/minima in the wrong order

Edge Cases

  • Exactly three elements → direct product
  • All positives
  • Mix of positives and negatives
  • Zeros present

Alternative Variations

  • Maximum product of k numbers instead of three
  • Maximum product subarray (contiguous)
  • Minimum product of three numbers
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;