Maximum Subsequence Score - Problem
Maximum Subsequence Score is a fascinating optimization problem that combines elements of greedy algorithms and heap management. You're given two arrays nums1 and nums2 of equal length n, and you need to select exactly k indices to maximize a special score.

Your score is calculated as: (sum of selected elements from nums1) × (minimum of selected elements from nums2).

For example, if you select indices [0, 2, 3], your score would be:
(nums1[0] + nums1[2] + nums1[3]) × min(nums2[0], nums2[2], nums2[3])

The challenge is finding which k indices give you the maximum possible score. This problem appears frequently in technical interviews at top tech companies and tests your ability to think strategically about optimization under constraints.

Input & Output

example_1.py — Basic Example
$ Input: nums1 = [1,3,3,2], nums2 = [2,1,3,4], k = 3
Output: 12
💡 Note: The optimal subsequence is indices [0,2,3] giving us (1+3+2) × min(2,3,4) = 6 × 2 = 12.
example_2.py — Larger Values
$ Input: nums1 = [4,2,3,1,1], nums2 = [7,5,10,9,6], k = 1
Output: 30
💡 Note: With k=1, we choose the single index that maximizes nums1[i] × nums2[i]. Index 2 gives us 3 × 10 = 30.
example_3.py — All Elements
$ Input: nums1 = [2,1,14,12], nums2 = [11,7,13,6], k = 4
Output: 174
💡 Note: We must select all indices: (2+1+14+12) × min(11,7,13,6) = 29 × 6 = 174.

Constraints

  • n == nums1.length == nums2.length
  • 1 ≤ n ≤ 105
  • 1 ≤ k ≤ n
  • 0 ≤ nums1[i], nums2[i] ≤ 105

Visualization

Tap to expand
Maximum Subsequence Score INPUT nums1: 1 3 3 2 idx 0 idx 1 idx 2 idx 3 nums2: 2 1 3 4 k = 3 (select 3 indices) Score Formula: sum(nums1[selected]) x min(nums2[selected]) ALGORITHM STEPS 1 Sort by nums2 DESC Pair indices, sort by nums2 idx3: n1=2, n2=4 idx2: n1=3, n2=3 idx0: n1=1, n2=2 idx1: n1=3, n2=1 2 Use Min-Heap Track k smallest nums1 3 Iterate sorted pairs Current nums2 is minimum 4 Calculate score sum x current_min After idx3,2,0 (k=3): heap=[1,2,3] sum=6 min=2 score=6x2=12 Try idx1: pop 1, add 3 score=8x1=8 (worse) FINAL RESULT Selected Indices: 0 idx 2 idx 3 idx Values at indices 0,2,3: nums1: [1, 3, 2] nums2: [2, 3, 4] sum=6, min=2 OUTPUT 12 [OK] Maximum Score Key Insight: By sorting indices by nums2 in descending order, we ensure that as we iterate, the current nums2 value is always the minimum among selected elements. The greedy approach uses a min-heap to maintain the k largest nums1 values seen so far, maximizing sum x min. TutorialsPoint - Maximum Subsequence Score | Greedy Approach
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
45.3K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen