Get Maximum Sum of Strengths
Updated
Problem statement
Given an array arr that contains n integers, the following operation can be performed on it any number of times (possibly zero):
Choose any index i where 0 ≤ i < n - 1 and swap arr[i] with arr[i + 1]. Each element of the array can be swapped at most once during the whole process.
The strength of index i is defined as arr[i] * (i + 1) using 0-based indexing. Find the maximum possible sum of the strength of all indices after optimal swaps. Mathematically, maximize the following:
Complete the function getMaximumSumOfStrengths in the editor below.
getMaximumSumOfStrengths has the following parameter:
int arr[n]: the initial array
long int: the maximum sum of strengths
Function
getMaximumSumOfStrengths(arr: int[]) → longExamples
Example 1
arr = [1, 9, 7, 3, 2]return = 66
It is optimal to swap (arr[2], arr[3]). The final array is arr[] = [1, 9, 3, 7, 2]. The sum of strengths (1*1 + 2*9 + 3*3 + 4*7 + 5*2) = 66, which is the maximum possible.
Example 2
arr = [2, 1, 4, 3]return = 30
It is optimal to swap (arr[0], arr[1]) and (arr[2], arr[3]). The final array is arr[] = [1, 2, 3, 4]. The sum of strengths (1*1 + 2*2 + 3*3 + 4*4) = 30, which is the maximum possible.
Example 3
arr = [1, 2, 5]return = 20
No swaps are needed as the array is already in optimal order. The sum of strengths (1*1 + 2*2 + 3*5) = 20, which is the maximum possible.
Constraints
1 ≤ n ≤ 10^51 ≤ arr[i] ≤ 10^5
More Salesforce interview questions
- Count Subarrays With Product Equal to KOA · Seen Dec 2026
- Get Minimum TimeOA · Seen Nov 2026
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
- Get Message StatusOA · Seen Nov 2026
Similar questions at other companies
- Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
- Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
- Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
- Maximize Minimum Deployment DifficultyJP Morgan Chase · OAJP Morgan Chase interview questions
- Latest Arrival to Catch a BusAutodesk · OAAutodesk interview questions
- Maximize Center A Package BenefitAmazon · OA
- EV Charging Cost OptimizationTata Consultancy Services · OATata Consultancy Services interview questions
- Maximize Workday EarningsCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions