Ctrl K

Best Product of Three

Easy
You are given an integer array values. Choose three elements at different indices so that their product is as large as possible, and return that product. Keep in mind that the numbers may be negative.
Example 1
values=[-8, -5, 1, 2, 4] → 160

Two negatives make a positive: (−8) · (−5) · 4 = 160, which beats 1 · 2 · 4 = 8.

Example 2
values=[-4, -3, -2, -1] → -6

All numbers are negative, so any product of three is negative too. The one closest to zero is (−3) · (−2) · (−1) = −6.

Example 3
values=[0, 6, 2, 3] → 36
Constraints
  • 3 ≤ len(values) ≤ 10⁴
  • -1000 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Lost Ticket EasyTriangle From Sticks EasyHow a Rumor Spreads EasyTriangle From Three Sticks EasyWhere the Friends Should Meet Hard
loading Python…
Input
values=[-8, -5, 1, 2, 4]
Expected
160

Run checks the first 3 tests in your browser. Submit checks all 114 tests on the server, hidden ones included.