Problem · Array
Maximum Weighted Sum After Left Rotations
Learn this problemProblem statement
You are given a non-empty integer array nums of length n.
For an array a, define its weighted sum as a[0] * 1 + a[1] * 2 + ... + a[n - 1] * n.
Consider the original array and every array obtained by left-rotating nums. Return the maximum weighted sum among all n rotations.
Function
maximumWeightedLeftRotation(nums: int[]) → longExamples
Example 1
nums = [3,2,1]return = 13The rotations have weighted sums 10 for [3,2,1], 13 for [2,1,3], and 13 for [1,3,2]. The maximum is 13.
Example 2
nums = [8,3,1,2]return = 43The left rotation [3,1,2,8] has weighted sum 3 + 2 + 6 + 32 = 43, which is the maximum.
Example 3
nums = [-5]return = -5A length-one array has only its original rotation.
Constraints
1 <= nums.length <= 200000-2147483648 <= nums[i] <= 2147483647- Every rotation's weighted sum fits in a signed 64-bit integer.