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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given two integer arrays nums1 and nums2 of lengths n and m respectively, and an integer k.
You must choose exactly k pairs of indices (i1, j1), (i2, j2), ..., (ik, jk) such that:
0 <= i1 < i2 < ... < ik < n0 <= j1 < j2 < ... < jk < mFor each chosen pair (i, j), you gain a score of nums1[i] * nums2[j].
The total score is the sum of the products of all selected pairs.
Return an integer representing the maximum achievable total score.
Example 1:
Input: nums1 = [1,3,2], nums2 = [4,5,1], k = 2
Output: 22
Explanation:
One optimal choice of index pairs is:
(i1, j1) = (1, 0) which scores 3 * 4 = 12(i2, j2) = (2, 1) which scores 2 * 5 = 10This gives a total score of 12 + 10 = 22.
Example 2:
Input: nums1 = [-2,0,5], nums2 = [-3,4,-1,2], k = 2
Output: 26
Explanation:
One optimal choice of index pairs is:
(i1, j1) = (0, 0) which scores -2 * -3 = 6(i2, j2) = (2, 1) which scores 5 * 4 = 20The total score is 6 + 20 = 26.
Example 3:
Input: nums1 = [-3,-2], nums2 = [1,2], k = 2
Output: -7
Explanation:
The optimal choice of index pairs is:
(i1, j1) = (0, 0) which scores -3 * 1 = -3(i2, j2) = (1, 1) which scores -2 * 2 = -4The total score is -3 + (-4) = -7.
Constraints:
1 <= n == nums1.length <= 1001 <= m == nums2.length <= 100-106 <= nums1[i], nums2[i] <= 1061 <= k <= min(n, m)Problem summary: You are given two integer arrays nums1 and nums2 of lengths n and m respectively, and an integer k. You must choose exactly k pairs of indices (i1, j1), (i2, j2), ..., (ik, jk) such that: 0 <= i1 < i2 < ... < ik < n 0 <= j1 < j2 < ... < jk < m For each chosen pair (i, j), you gain a score of nums1[i] * nums2[j]. The total score is the sum of the products of all selected pairs. Return an integer representing the maximum achievable total score.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[1,3,2] [4,5,1] 2
[-2,0,5] [-3,4,-1,2] 2
[-3,-2] [1,2] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3836: Maximum Score Using Exactly K Pairs
class Solution {
public long maxScore(int[] nums1, int[] nums2, int K) {
int n = nums1.length, m = nums2.length;
long NEG = Long.MIN_VALUE / 4;
long[][][] f = new long[n + 1][m + 1][K + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
Arrays.fill(f[i][j], NEG);
}
}
f[0][0][0] = 0;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= K; k++) {
if (i > 0) {
f[i][j][k] = Math.max(f[i][j][k], f[i - 1][j][k]);
}
if (j > 0) {
f[i][j][k] = Math.max(f[i][j][k], f[i][j - 1][k]);
}
if (i > 0 && j > 0 && k > 0) {
f[i][j][k] = Math.max(f[i][j][k],
f[i - 1][j - 1][k - 1] + (long) nums1[i - 1] * nums2[j - 1]);
}
}
}
}
return f[n][m][K];
}
}
// Accepted solution for LeetCode #3836: Maximum Score Using Exactly K Pairs
func maxScore(nums1 []int, nums2 []int, K int) int64 {
n, m := len(nums1), len(nums2)
NEG := int64(math.MinInt64 / 4)
f := make([][][]int64, n+1)
for i := 0; i <= n; i++ {
f[i] = make([][]int64, m+1)
for j := 0; j <= m; j++ {
f[i][j] = make([]int64, K+1)
for k := 0; k <= K; k++ {
f[i][j][k] = NEG
}
}
}
f[0][0][0] = 0
for i := 0; i <= n; i++ {
for j := 0; j <= m; j++ {
for k := 0; k <= K; k++ {
if i > 0 {
f[i][j][k] = max(f[i][j][k], f[i-1][j][k])
}
if j > 0 {
f[i][j][k] = max(f[i][j][k], f[i][j-1][k])
}
if i > 0 && j > 0 && k > 0 {
f[i][j][k] = max(
f[i][j][k],
f[i-1][j-1][k-1]+int64(nums1[i-1])*int64(nums2[j-1]),
)
}
}
}
}
return f[n][m][K]
}
# Accepted solution for LeetCode #3836: Maximum Score Using Exactly K Pairs
class Solution:
def maxScore(self, nums1: List[int], nums2: List[int], K: int) -> int:
n, m = len(nums1), len(nums2)
f = [[[-inf] * (K + 1) for _ in range(m + 1)] for _ in range(n + 1)]
f[0][0][0] = 0
for i in range(n + 1):
for j in range(m + 1):
for k in range(K + 1):
if i > 0:
f[i][j][k] = max(f[i][j][k], f[i - 1][j][k])
if j > 0:
f[i][j][k] = max(f[i][j][k], f[i][j - 1][k])
if i > 0 and j > 0 and k > 0:
f[i][j][k] = max(
f[i][j][k],
f[i - 1][j - 1][k - 1] + nums1[i - 1] * nums2[j - 1],
)
return f[n][m][K]
// Accepted solution for LeetCode #3836: Maximum Score Using Exactly K Pairs
// 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 #3836: Maximum Score Using Exactly K Pairs
// class Solution {
// public long maxScore(int[] nums1, int[] nums2, int K) {
// int n = nums1.length, m = nums2.length;
// long NEG = Long.MIN_VALUE / 4;
// long[][][] f = new long[n + 1][m + 1][K + 1];
// for (int i = 0; i <= n; i++) {
// for (int j = 0; j <= m; j++) {
// Arrays.fill(f[i][j], NEG);
// }
// }
// f[0][0][0] = 0;
// for (int i = 0; i <= n; i++) {
// for (int j = 0; j <= m; j++) {
// for (int k = 0; k <= K; k++) {
// if (i > 0) {
// f[i][j][k] = Math.max(f[i][j][k], f[i - 1][j][k]);
// }
// if (j > 0) {
// f[i][j][k] = Math.max(f[i][j][k], f[i][j - 1][k]);
// }
// if (i > 0 && j > 0 && k > 0) {
// f[i][j][k] = Math.max(f[i][j][k],
// f[i - 1][j - 1][k - 1] + (long) nums1[i - 1] * nums2[j - 1]);
// }
// }
// }
// }
// return f[n][m][K];
// }
// }
// Accepted solution for LeetCode #3836: Maximum Score Using Exactly K Pairs
function maxScore(nums1: number[], nums2: number[], K: number): number {
const n = nums1.length,
m = nums2.length;
const NEG = -1e18;
const f = Array.from({ length: n + 1 }, () =>
Array.from({ length: m + 1 }, () => Array(K + 1).fill(NEG)),
);
f[0][0][0] = 0;
for (let i = 0; i <= n; i++) {
for (let j = 0; j <= m; j++) {
for (let k = 0; k <= K; k++) {
if (i > 0) {
f[i][j][k] = Math.max(f[i][j][k], f[i - 1][j][k]);
}
if (j > 0) {
f[i][j][k] = Math.max(f[i][j][k], f[i][j - 1][k]);
}
if (i > 0 && j > 0 && k > 0) {
f[i][j][k] = Math.max(
f[i][j][k],
f[i - 1][j - 1][k - 1] + nums1[i - 1] * nums2[j - 1],
);
}
}
}
}
return f[n][m][K];
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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.