Element Swapping
Learn this problemProblem statement
A software development firm is hiring engineers and used the following challenge in its online test.
Given an array arr that contains n integers, the following operation can be performed on it any number of times (possibly zero):
i (0 ≤ i and swap arr[i] and arr[i + 1]
The strength of an index i is defined as arr[i] * (i + 1) using 0-based indexing. Find the maximum possible sum of the strengths of all indices after optimal swaps. Mathematically, maximize the following:
Function
getMaximumSumOfStrengths(arr: int[]) → long
Complete the function getMaximumSumOfStrengths in the editor below.
getMaximumSumOfStrengths has the following parameter:
int arr[n]: the initial array
Returns
long integer: the maximum possible sum of strengths of all indices after the operations are applied optimally
Examples
Example 1
arr = [2, 1, 4, 3]return = 30Constraints
- 1 ≤
n≤ 10^5 - 1 ≤
arr[i]≤ 10^5