3584. Maximum Product of First and Last Elements of a Subsequence Medium
1/**
2 * [3584] Maximum Product of First and Last Elements of a Subsequence
3 *
4 * You are given an integer array nums and an integer m.
5 * Return the maximum product of the first and last elements of any <span data-keyword="subsequence-array">subsequence</span> of nums of size m.
6 *
7 * <strong class="example">Example 1:
8 * <div class="example-block">
9 * Input: <span class="example-io">nums = [-1,-9,2,3,-2,-3,1], m = 1</span>
10 * Output: <span class="example-io">81</span>
11 * Explanation:
12 * The subsequence [-9] has the largest product of the first and last elements: -9 * -9 = 81. Therefore, the answer is 81.
13 * </div>
14 * <strong class="example">Example 2:
15 * <div class="example-block">
16 * Input: <span class="example-io">nums = [1,3,-5,5,6,-4], m = 3</span>
17 * Output: <span class="example-io">20</span>
18 * Explanation:
19 * The subsequence [-5, 6, -4] has the largest product of the first and last elements.
20 * </div>
21 * <strong class="example">Example 3:
22 * <div class="example-block">
23 * Input: <span class="example-io">nums = [2,-1,2,-6,5,2,-5,7], m = 2</span>
24 * Output: <span class="example-io">35</span>
25 * Explanation:
26 * The subsequence [5, 7] has the largest product of the first and last elements.
27 * </div>
28 *
29 * Constraints:
30 *
31 * 1 <= nums.length <= 10^5
32 * -10^5 <= nums[i] <= 10^5
33 * 1 <= m <= nums.length
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/maximum-product-of-first-and-last-elements-of-a-subsequence/
39// discuss: https://leetcode.com/problems/maximum-product-of-first-and-last-elements-of-a-subsequence/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn maximum_product(nums: Vec<i32>, m: i32) -> i64 {
45
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_3584() {
57 }
58}
59Back
© 2026 bowen.ge All Rights Reserved.