Quick Overview

An array optimization problem: maximize the sum of each element times its index when any two elements may be swapped but every element can take part in at most one swap. It tests modeling the at-most-once rule precisely and finding the best set of disjoint swaps within small input limits.

Maximize Index-Weighted Array Sum When Each Element Swaps at Most Once

Company: Visa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Online Assessment

You are given an integer array `nums`. You may swap elements, choosing any two positions for each swap, but every element may take part in at most one swap. After the swaps, the score of the array is the sum of `nums[i] * i` over all positions `i`. Return the maximum score you can reach. ### Function Signature ```python def max_index_weighted_sum(nums: list[int]) -> int: ``` ### Rules - Positions are 0-indexed, so the score is `0 * nums[0] + 1 * nums[1] + ... + (n - 1) * nums[n - 1]`, computed on the array after all swaps. - A swap exchanges the elements at two different positions. The two positions do not have to be adjacent. - Each element takes part in at most one swap, so the swaps you choose use pairwise disjoint pairs of positions, and their order does not matter. Making no swap at all is allowed. - Return the maximum score as an integer. ### Constraints - `1 <= len(nums) <= 16` - `0 <= nums[i] <= 10^6` - The answer is at most `1.2 * 10^8`, so it fits in a 32-bit signed integer. ### Examples **Example 1** ```text Input: nums = [2, 1, 4, 3] Output: 20 ``` Swapping positions 0 and 1, and positions 2 and 3, gives `[1, 2, 3, 4]` with score `0 + 2 + 6 + 12 = 20`. No arrangement of these values scores more. **Example 2** ```text Input: nums = [3, 1, 2] Output: 7 ``` The sorted arrangement `[1, 2, 3]` would score 8, but reaching it moves all three elements around a cycle, which needs two swaps that share an element. Swapping positions 0 and 1 gives `[1, 3, 2]` with score `0 + 3 + 4 = 7`; swapping positions 0 and 2 gives `[2, 1, 3]`, also 7. **Example 3** ```text Input: nums = [3, 4, 1, 2] Output: 20 ``` The original score is `0 + 4 + 2 + 6 = 12`. Swapping positions 0 and 2, and positions 1 and 3, gives `[1, 2, 3, 4]` with score 20.

Overview: An array optimization problem: maximize the sum of each element times its index when any two elements may be swapped but every element can take part in at most one swap. It tests modeling the at-most-once rule precisely and finding the best set of disjoint swaps within small input limits.

Read the full Visa Software Engineer interview experience this question came from

You are given an integer array `nums` of length `n`. You may swap elements: each swap exchanges the elements at two different positions, and the two positions do not have to be adjacent. Every element may take part in at most one swap, so the swaps you choose use pairwise disjoint pairs of positions and their order does not matter. Making no swap at all is allowed. After the swaps, the score of the array is the sum of `nums[i] * i` over all positions `i`. Positions are 0-indexed, so the score is `0 * nums[0] + 1 * nums[1] + ... + (n - 1) * nums[n - 1]`, computed on the array after all swaps. Return the maximum score you can reach, as an integer. ### Constraints - `1 <= len(nums) <= 16` - `0 <= nums[i] <= 10^6` - The answer is at most `1.2 * 10^8`, so it fits in a 32-bit signed integer. No value, including the answer, exceeds `2^31 - 1`, so Java and C++ use `int`. ### Examples **Example 1** ```text Input: nums = [2, 1, 4, 3] Output: 20 ``` Swapping positions 0 and 1, and positions 2 and 3, gives `[1, 2, 3, 4]` with score `0 + 2 + 6 + 12 = 20`. No arrangement of these values scores more. **Example 2** ```text Input: nums = [3, 1, 2] Output: 7 ``` The sorted arrangement `[1, 2, 3]` would score 8, but reaching it moves all three elements around a cycle, which needs two swaps that share an element. Swapping positions 0 and 1 gives `[1, 3, 2]` with score `0 + 3 + 4 = 7`; swapping positions 0 and 2 gives `[2, 1, 3]`, also 7.

Constraints

  • 1 <= len(nums) <= 16
  • 0 <= nums[i] <= 10^6
  • The answer is at most 1.2 * 10^8, so it fits in a 32-bit signed integer.

Examples

Input: ([1000000],)

Expected Output: 0

Explanation: Singleton at the value bound: position 0 has weight 0 and no swap is possible, so the score is 0.

Input: ([1, 2],)

Expected Output: 2

Explanation: Length 2 already in order: swapping would drop the score to 1, so make no swap; score 2.

Hints

  1. Work out how one swap of positions i < j changes the score. The change depends only on the two values and on the distance j - i, and swaps on disjoint positions do not affect each other's change.
  2. Sorting ascending gives an upper bound on the score, but Example 2 shows it is not always reachable when each element may join at most one swap.
  3. The array has at most 16 positions, so comparing many different collections of non-overlapping swaps is affordable.

Loading coding console...

Show the approach

Approach

Swapping positions i < j changes the score by (j - i) * (nums[i] - nums[j]): the larger value moves to the larger index exactly when nums[i] > nums[j]. Swaps on disjoint position pairs touch disjoint positions, so their changes add independently. The answer is therefore the original score plus the largest total gain over a set of pairwise disjoint pairs, which is a maximum-weight matching on at most 16 positions.

Let dp[mask] be the best total gain using only the positions in mask. For a nonempty mask, take its lowest position low: either low is not swapped (dp[mask without low]) or it is paired with another position j in mask (gain(low, j) + dp[mask without low and j]). Every set of disjoint pairs inside mask falls into exactly one of these branches, so by induction on the size of mask, dp[mask] is optimal; dp[0] = 0 covers making no swap. Only pairs with positive gain are tried, since removing a pair with non-positive gain never lowers the total. The result is the original score plus dp over all positions.

Edge cases: a single element has no pair and scores 0; ascending or all-equal arrays have no positive-gain pair, so the original score is returned; a cycle of three or more misplaced values (for example [3, 1, 2]) cannot be fully sorted, which the matching search handles without special cases. Every intermediate value is the gain of a real swap set, so it never exceeds 1.2 * 10^8 and 32-bit integers suffice.

Time complexity:
O(2^n * n)
Space complexity:
O(2^n + n^2)