Maximum Product of First and Last Elements of a Subsequence

You are given an integer array nums and an integer m.

Return the maximum product of the first and last elements of any subsequence of nums of size m.

Example 1
Inputnums = [-1,-9,2,3,-2,-3,1], m = 1
Output81
The subsequence [-9] has the largest product of the first and last elements: -9 * -9 = 81.
Example 2
Inputnums = [1,3,-5,5,6,-4], m = 3
Output20
The subsequence [-5, 6, -4] has the largest product of the first and last elements.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^5 <= nums[i] <= 10^5
  • 1 <= m <= nums.length

Asked at 1 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate