09 Sept 2026Go / Python / TypeScriptEasy

Maximum Product of Three Numbers

Find the largest product obtainable from three integers in an array.

After sorting, compare the product of the three largest values with the product of the two smallest values and the largest value.

complexity

O(n log n) time and O(n) or O(log n) sorting space depending on the language runtime.

solution files

  • Go maximum-product-of-three-numbers/solution.go
  • Python maximum-product-of-three-numbers/solution.py
  • TypeScript maximum-product-of-three-numbers/solution.ts

Solution files

Gomaximum-product-of-three-numbers/solution.go
package main

import "sort"

func maximumProduct(nums []int) int {
	values := append([]int(nil), nums...)
	sort.Ints(values)
	last := len(values) - 1
	first := values[last] * values[last-1] * values[last-2]
	second := values[0] * values[1] * values[last]
	if second > first {
		return second
	}
	return first
}
Pythonmaximum-product-of-three-numbers/solution.py
class Solution:
    def maximumProduct(self, nums: list[int]) -> int:
        values = sorted(nums)
        return max(values[-class="syntax-number">1] * values[-class="syntax-number">2] * values[-class="syntax-number">3], values[class="syntax-number">0] * values[class="syntax-number">1] * values[-class="syntax-number">1])
TypeScriptmaximum-product-of-three-numbers/solution.ts
function maximumProduct(nums: number[]): number {
  const sorted = [...nums].sort((left, right) => left - right);
  const last = sorted.length - class="syntax-number">1;
  return Math.max(
    sorted[last] * sorted[last - class="syntax-number">1] * sorted[last - class="syntax-number">2],
    sorted[class="syntax-number">0] * sorted[class="syntax-number">1] * sorted[last],
  );
}