Find the maximum possible product of any three numbers in an integer array.
Given an integer array nums, choose any three distinct elements and multiply them together. Return the largest product you can make.
The key challenge is that negative numbers can change the result dramatically, so the optimal triple is not always the three largest values.
Input Format
- An integer array
numswith at least 3 elements. - Each element is an integer that may be negative, zero, or positive.
Output Format
- Return a single integer: the maximum product obtainable from any three distinct elements in
nums.
Constraints
3 <= nums.length- Values may be negative, zero, or positive.
- Use 64-bit arithmetic in reasoning if needed, since intermediate products may exceed 32-bit range.
Example 1
Input
nums = [1, 2, 3]
Output
6
Explanation
The only triple is 1 × 2 × 3 = 6.
Example 2
Input
nums = [1, 2, 3, 4]
Output
24
Explanation
The best choice is 2 × 3 × 4 = 24.
Show 1 more example
Example 3
Input
nums = [-10, -10, 5, 2]
Output
500
Explanation
Using the two negative numbers helps: (-10) × (-10) × 5 = 500.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.