Show problems as
Classic problem #628EasyArrayMathSorting

Maximum Product of Three Numbers

Real-world version: Largest Triple Drift
Tracked in this browser only
Read Maximum Product of Three Numbers on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A calibration rig logs how far a gauge drifted each shift. drifts[i] is the signed drift for shift i, negative when the gauge fell short and positive when it overshot.

The rig's stress score for three shifts is the product of their three drift values. Pick three shifts at three different positions and return the largest stress score reachable.

Note that two badly negative drifts multiply into a large positive number, so the three biggest values are not always the winning pick.

Examples

Example 1

Input
drifts = [3, 5, 1, 8]
Output
120

Every value is positive, so the three largest, 3, 5 and 8, give the best score of 120.

Example 2

Input
drifts = [-9, -8, 1, 2]
Output
144

Taking -9 and -8 together cancels both signs, and pairing them with 2 beats the 1 * 2 * -8 available from the three largest values.

Example 3

Input
drifts = [-5, -4, -3]
Output
-60

Only one triple exists, and its score is negative; that is still the answer.

Constraints

  • 3 <= drifts.length <= 10^4
  • -1000 <= drifts[i] <= 1000

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def largest_triple_drift(drifts: list[int]) -> int:
Java
public int largestTripleDrift(int[] drifts)