Maximize Strength with Disjoint Adjacent Swaps
Updated
Problem statement
You are given an integer array arr. You may swap two elements that sit next to each other, as many times as you like. But each element of the original array can take part in at most one swap. So no two of your swaps may touch the same element.
After the swaps, the strength is sum(arr[i] * (i + 1)). Here i is the index counted from zero, so each value is multiplied by its position counted from one.
Return the largest strength you can get.
Function
maxWeightedStrengthAfterSwaps(arr: int[]) → longExamples
Example 1
arr = [3,1,2]return = 13Swapping the first two elements gives [1, 3, 2] and strength 1 + 6 + 6 = 13.
Example 2
arr = [5,1,4,2]return = 33Swap the first two elements, and also swap the last two. No element is used in more than one swap. This 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 interview questions
Similar questions at other companies
- Card Game: Best First-Player ScoreShopify · OAShopify interview questions
- Maximum Game Score with Fixed Forward JumpsStripe · OAStripe interview questions
- 0/1 KnapsackConfluent · OAConfluent interview questions
- Minimum Cost to Tile a Strip with Widths 1, 2, and 3Infosys · OAInfosys interview questions
- Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
- Maximum Subarray SumStraive · OA
- Longest Increasing Price SubsequenceOpenAI · OAOpenAI interview questions
- Lowest-Cost Shirt Color ScheduleElevenLabs · OAElevenLabs interview questions
- Maximum Game Board ScoreJP Morgan Chase · OAJP Morgan Chase interview questions
- API Bandwidth AllocationArista Networks · OAArista Networks interview questions