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 nums, an array of positive integers of size 2 * n. You must perform n operations on this array.
In the ith operation (1-indexed), you will:
x and y.i * gcd(x, y).x and y from nums.Return the maximum score you can receive after performing n operations.
The function gcd(x, y) is the greatest common divisor of x and y.
Example 1:
Input: nums = [1,2] Output: 1 Explanation: The optimal choice of operations is: (1 * gcd(1, 2)) = 1
Example 2:
Input: nums = [3,4,6,8] Output: 11 Explanation: The optimal choice of operations is: (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
Example 3:
Input: nums = [1,2,3,4,5,6] Output: 14 Explanation: The optimal choice of operations is: (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
Constraints:
1 <= n <= 7nums.length == 2 * n1 <= nums[i] <= 106Problem summary: You are given nums, an array of positive integers of size 2 * n. You must perform n operations on this array. In the ith operation (1-indexed), you will: Choose two elements, x and y. Receive a score of i * gcd(x, y). Remove x and y from nums. Return the maximum score you can receive after performing n operations. The function gcd(x, y) is the greatest common divisor of x and y.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming · Backtracking · Bit Manipulation
[1,2]
[3,4,6,8]
[1,2,3,4,5,6]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1799: Maximize Score After N Operations
class Solution {
public int maxScore(int[] nums) {
int m = nums.length;
int[][] g = new int[m][m];
for (int i = 0; i < m; ++i) {
for (int j = i + 1; j < m; ++j) {
g[i][j] = gcd(nums[i], nums[j]);
}
}
int[] f = new int[1 << m];
for (int k = 0; k < 1 << m; ++k) {
int cnt = Integer.bitCount(k);
if (cnt % 2 == 0) {
for (int i = 0; i < m; ++i) {
if (((k >> i) & 1) == 1) {
for (int j = i + 1; j < m; ++j) {
if (((k >> j) & 1) == 1) {
f[k] = Math.max(
f[k], f[k ^ (1 << i) ^ (1 << j)] + cnt / 2 * g[i][j]);
}
}
}
}
}
}
return f[(1 << m) - 1];
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #1799: Maximize Score After N Operations
func maxScore(nums []int) int {
m := len(nums)
g := [14][14]int{}
for i := 0; i < m; i++ {
for j := i + 1; j < m; j++ {
g[i][j] = gcd(nums[i], nums[j])
}
}
f := make([]int, 1<<m)
for k := 0; k < 1<<m; k++ {
cnt := bits.OnesCount(uint(k))
if cnt%2 == 0 {
for i := 0; i < m; i++ {
if k>>i&1 == 1 {
for j := i + 1; j < m; j++ {
if k>>j&1 == 1 {
f[k] = max(f[k], f[k^(1<<i)^(1<<j)]+cnt/2*g[i][j])
}
}
}
}
}
}
return f[1<<m-1]
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #1799: Maximize Score After N Operations
class Solution:
def maxScore(self, nums: List[int]) -> int:
m = len(nums)
f = [0] * (1 << m)
g = [[0] * m for _ in range(m)]
for i in range(m):
for j in range(i + 1, m):
g[i][j] = gcd(nums[i], nums[j])
for k in range(1 << m):
if (cnt := k.bit_count()) % 2 == 0:
for i in range(m):
if k >> i & 1:
for j in range(i + 1, m):
if k >> j & 1:
f[k] = max(
f[k],
f[k ^ (1 << i) ^ (1 << j)] + cnt // 2 * g[i][j],
)
return f[-1]
// Accepted solution for LeetCode #1799: Maximize Score After N Operations
/**
* [1799] Maximize Score After N Operations
*
* You are given nums, an array of positive integers of size 2 * n. You must perform n operations on this array.
* In the i^th operation (1-indexed), you will:
*
* Choose two elements, x and y.
* Receive a score of i * gcd(x, y).
* Remove x and y from nums.
*
* Return the maximum score you can receive after performing n operations.
* The function gcd(x, y) is the greatest common divisor of x and y.
*
* Example 1:
*
* Input: nums = [1,2]
* Output: 1
* Explanation: The optimal choice of operations is:
* (1 * gcd(1, 2)) = 1
*
* Example 2:
*
* Input: nums = [3,4,6,8]
* Output: 11
* Explanation: The optimal choice of operations is:
* (1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
*
* Example 3:
*
* Input: nums = [1,2,3,4,5,6]
* Output: 14
* Explanation: The optimal choice of operations is:
* (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
*
*
* Constraints:
*
* 1 <= n <= 7
* nums.length == 2 * n
* 1 <= nums[i] <= 10^6
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximize-score-after-n-operations/
// discuss: https://leetcode.com/problems/maximize-score-after-n-operations/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn max_score(nums: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_1799_example_1() {
let nums = vec![1, 2];
let result = 1;
assert_eq!(Solution::max_score(nums), result);
}
#[test]
#[ignore]
fn test_1799_example_2() {
let nums = vec![3, 4, 6, 8];
let result = 11;
assert_eq!(Solution::max_score(nums), result);
}
#[test]
#[ignore]
fn test_1799_example_3() {
let nums = vec![1, 2, 3, 4, 5, 6];
let result = 14;
assert_eq!(Solution::max_score(nums), result);
}
}
// Accepted solution for LeetCode #1799: Maximize Score After N Operations
function maxScore(nums: number[]): number {
const m = nums.length;
const f: number[] = new Array(1 << m).fill(0);
const g: number[][] = new Array(m).fill(0).map(() => new Array(m).fill(0));
for (let i = 0; i < m; ++i) {
for (let j = i + 1; j < m; ++j) {
g[i][j] = gcd(nums[i], nums[j]);
}
}
for (let k = 0; k < 1 << m; ++k) {
const cnt = bitCount(k);
if (cnt % 2 === 0) {
for (let i = 0; i < m; ++i) {
if ((k >> i) & 1) {
for (let j = i + 1; j < m; ++j) {
if ((k >> j) & 1) {
const t = f[k ^ (1 << i) ^ (1 << j)] + ~~(cnt / 2) * g[i][j];
f[k] = Math.max(f[k], t);
}
}
}
}
}
}
return f[(1 << m) - 1];
}
function gcd(a: number, b: number): number {
return b ? gcd(b, a % b) : a;
}
function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.