Problem · Array

Maximize Strength with Disjoint Adjacent Swaps

Learn this problem
MediumAdyen logoAdyenFULLTIMEOA

Problem statement

You are given an integer array arr. You may swap adjacent elements any number of times, but each original array element may participate in at most one swap. Therefore, chosen adjacent swaps cannot share an index.

After all swaps, the strength is sum(arr[i] * (i + 1)), using zero-based array indices and one-based position multipliers.

Return the maximum possible strength.

Function

maxWeightedStrengthAfterSwaps(arr: int[]) → long

Examples

Example 1

arr = [3,1,2]return = 13

Swapping the first two elements gives [1, 3, 2] and strength 1 + 6 + 6 = 13.

Example 2

arr = [5,1,4,2]return = 33

Swapping both disjoint pairs gives [1, 5, 2, 4] and strength 1 + 10 + 6 + 16 = 33.

Constraints

  • 1 <= arr.length <= 8,000.
  • -10^9 <= arr[i] <= 10^9.

More Adyen problems

drafts saved locally
public long maxWeightedStrengthAfterSwaps(int[] arr) {
    // Return the maximum weighted strength.
}
arr[3,1,2]
expected13
checking account