FastPrepMaximum Product of Three Integers

Maximum Product of Three Integers

Pelatro logoPelatro● EasyFULLTIMEONSITE INTERVIEW

Problem statement

Given an integer array nums, choose exactly three distinct indices and return the largest possible product of the values at those indices.

Values may be negative, zero, or positive. Equal values at different indices are separate choices.

Function

maximumProductOfThree(nums: int[]) → long

Examples

Example 1

nums = [-10,-10,5,2]return = 500

Choose the two entries equal to -10 and the entry equal to 5. Their product is 500. The other three-index products are 200, -100, and -100, so the maximum is 500.

Example 2

nums = [1,2,3]return = 6

There is only one possible set of three indices. Its product is 1 * 2 * 3 = 6.

Example 3

nums = [-5,-4,-3,-2]return = -24

The four possible products are -60, -40, -30, and -24. The largest is -24, obtained from -4, -3, and -2.

Constraints

  • 3 <= nums.length <= 2000.
  • -100000 <= nums[i] <= 100000.
  • Select exactly three distinct indices. Equal values at different indices may all be selected.
  • The answer is an exact integer with magnitude at most 10^15. Use a signed 64-bit return type where required.

More Pelatro problems

See Pelatro hiring insights
Saved
public long maximumProductOfThree(int[] nums) {
    // Write your solution here.
}
nums[-10,-10,5,2]
expected500
Checking account…