Pair Values with Increasing GCD Multipliers

Quick Overview

Pair Values with Increasing GCD Multipliers. The positive array nums has length 2*n. Perform exactly n operations. On operation i, numbered from one, choose and remove two remaining entries x and y and earn i*gcd(x,y).

Pair Values with Increasing GCD Multipliers

Company: Groww

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem The positive array `nums` has length `2*n`. Perform exactly n operations. On operation i, numbered from one, choose and remove two remaining entries x and y and earn `i*gcd(x,y)`. Return the greatest total score over all pairings and operation orders. ```cpp class Solution { public: int maxScore(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [2,4] Output: 2 Explanation: The only operation earns gcd(2,4)=2. ``` **Example 2:** ```text nums = [2,3,4,9] Output: 8 Explanation: Pair 2 with 4 first for 2, then 3 with 9 for 2*3=6. The other perfect pairings have both gcds equal to 1. ``` ### Constraints - `1 <= n <= 7` and `nums.length == 2*n`. - `1 <= nums[i] <= 10^6`. ### Clarifying Questions Pair order matters because the multiplier grows. Equal values at different positions remain distinct available occurrences. ```hint Inspect the structure Which choices determine one operation's reward, and how are repeated equal values treated as input occurrences? ``` ### Reasoning checkpoints - Account for both the pairing and operation order when maximizing the weighted score. - Use each input occurrence once, including repeated values at different positions. ### Optional extensions Why is greedily committing the largest current gcd insufficient without considering the remaining matching?

Overview: Pair Values with Increasing GCD Multipliers. The positive array nums has length 2*n. Perform exactly n operations. On operation i, numbered from one, choose and remove two remaining entries x and y and earn i*gcd(x,y).

Groww logo
Groww
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The positive array nums has length 2*n. Perform exactly n operations. On operation i, numbered from one, choose and remove two remaining entries x and y and earn i*gcd(x,y). Return the greatest total score over all pairings and operation orders.

class Solution {
public:
    int maxScore(vector<int>& nums);
};

Examples

Example 1:

nums = [2,4]
Output: 2
Explanation: The only operation earns gcd(2,4)=2.

Example 2:

nums = [2,3,4,9]
Output: 8
Explanation: Pair 2 with 4 first for 2, then 3 with 9 for 2*3=6. The other perfect pairings have both gcds equal to 1.

Constraints

  • 1 <= n <= 7 and nums.length == 2*n .
  • 1 <= nums[i] <= 10^6 .

Clarifying Questions Guidance

Pair order matters because the multiplier grows. Equal values at different positions remain distinct available occurrences.

Reasoning checkpoints

  • Account for both the pairing and operation order when maximizing the weighted score.
  • Use each input occurrence once, including repeated values at different positions.

Optional extensions

Why is greedily committing the largest current gcd insufficient without considering the remaining matching?