Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
Input: nums = [-1,-9,2,3,-2,-3,1], m = 1
Output: 81
Explanation:
The subsequence [-9] has the largest product of the first and last elements: -9 * -9 = 81. Therefore, the answer is 81.
Example 2:
Input: nums = [1,3,-5,5,6,-4], m = 3
Output: 20
Explanation:
The subsequence [-5, 6, -4] has the largest product of the first and last elements.
Example 3:
Input: nums = [2,-1,2,-6,5,2,-5,7], m = 2
Output: 35
Explanation:
The subsequence [5, 7] has the largest product of the first and last elements.
Constraints:
1 <= nums.length <= 105-105 <= nums[i] <= 1051 <= m <= nums.lengthProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[-1,-9,2,3,-2,-3,1] 1
[1,3,-5,5,6,-4] 3
[2,-1,2,-6,5,2,-5,7] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3584: Maximum Product of First and Last Elements of a Subsequence
class Solution {
public long maximumProduct(int[] nums, int m) {
long ans = Long.MIN_VALUE;
int mx = Integer.MIN_VALUE;
int mi = Integer.MAX_VALUE;
for (int i = m - 1; i < nums.length; ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
}
return ans;
}
}
// Accepted solution for LeetCode #3584: Maximum Product of First and Last Elements of a Subsequence
func maximumProduct(nums []int, m int) int64 {
ans := int64(math.MinInt64)
mx := math.MinInt32
mi := math.MaxInt32
for i := m - 1; i < len(nums); i++ {
x := nums[i]
y := nums[i-m+1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
}
return ans
}
# Accepted solution for LeetCode #3584: Maximum Product of First and Last Elements of a Subsequence
class Solution:
def maximumProduct(self, nums: List[int], m: int) -> int:
ans = mx = -inf
mi = inf
for i in range(m - 1, len(nums)):
x = nums[i]
y = nums[i - m + 1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, x * mi, x * mx)
return ans
// Accepted solution for LeetCode #3584: Maximum Product of First and Last Elements of a Subsequence
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3584: Maximum Product of First and Last Elements of a Subsequence
// class Solution {
// public long maximumProduct(int[] nums, int m) {
// long ans = Long.MIN_VALUE;
// int mx = Integer.MIN_VALUE;
// int mi = Integer.MAX_VALUE;
// for (int i = m - 1; i < nums.length; ++i) {
// int x = nums[i];
// int y = nums[i - m + 1];
// mi = Math.min(mi, y);
// mx = Math.max(mx, y);
// ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3584: Maximum Product of First and Last Elements of a Subsequence
function maximumProduct(nums: number[], m: number): number {
let ans = Number.MIN_SAFE_INTEGER;
let mx = Number.MIN_SAFE_INTEGER;
let mi = Number.MAX_SAFE_INTEGER;
for (let i = m - 1; i < nums.length; i++) {
const x = nums[i];
const y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, x * mi, x * mx);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.