LeetCode 1799, Hard. Topics: Array, Math, Dynamic Programming, Backtracking, Bit Manipulation, Number Theory, Bitmask. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 20 parametrized pytest cases, and a playground notebook:
Problem
<p>You are given <code>nums</code>, an array of positive integers of size <code>2 * n</code>. You must perform <code>n</code> operations on this array.</p>
<p>In the <code>i<sup>th</sup></code> operation <strong>(1-indexed)</strong>, you will:</p>
<ul>
<li>Choose two elements, <code>x</code> and <code>y</code>.</li>
<li>Receive a score of <code>i * gcd(x, y)</code>.</li>
<li>Remove <code>x</code> and <code>y</code> from <code>nums</code>.</li>
</ul>
<p>Return <em>the maximum score you can receive after performing </em><code>n</code><em> operations.</em></p>
<p>The function <code>gcd(x, y)</code> is the greatest common divisor of <code>x</code> and <code>y</code>.</p>
Examples
Constraints
- 1 <= n <= 7
- nums.length == 2 * n
- 1 <= nums[i] <= 10^6
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026