You are given an integer array nums
of length n
.
Assume arrk
to be an array obtained by rotating nums
by k
positions clock-wise. We define the rotation function F
on nums
as follow:
F(k) = 0 * arrk[0] + 1 * arrk[1] + ... + (n - 1) * arrk[n - 1].
Return the maximum value of F(0), F(1), ..., F(n-1)
.
The test cases are generated so that the answer fits in a 32-bit integer.
Example 1:
Input: nums = [4,3,2,6] Output: 26 Explanation: F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25 F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16 F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23 F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26 So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.
Example 2:
Input: nums = [100] Output: 0
Constraints:
n == nums.length
1 <= n <= 105
-100 <= nums[i] <= 100
This problem asks to find the maximum value of the rotation function F(k) for an integer array nums
, where F(k) is the sum of the products of each element and its index after rotating the array k times. A naive approach would be to calculate F(k) for all k from 0 to n-1, but this would lead to O(n^2) time complexity. The optimized solution uses the relationship between consecutive rotations to achieve linear time complexity.
Core Idea:
The key observation is that the rotation function F(k) and F(k+1) are closely related. When we rotate the array by one position, the value of F changes in a predictable way. Let's analyze this change:
0*a[0] + 1*a[1] + 2*a[2] + ... + (n-1)*a[n-1]
(assuming a
is the rotated array).0*a[n-1] + 1*a[0] + 2*a[1] + ... + (n-1)*a[n-2]
Notice the difference: The last element a[n-1]
is now multiplied by 0 instead of n-1
, while other elements shift one position to the left with their multipliers increased by 1. To get F(k+1) from F(k):
(n-1)*a[n-1]
(the contribution of the last element in F(k)).sum(nums)
(because each element’s multiplier increased by 1).n*a[n-1]
(because the last element’s multiplier is now 0 instead of (n-1), we need to subtract the excess).Therefore, we can iteratively compute F(k+1) from F(k) in O(1) time. This avoids recalculating the entire sum for each rotation.
Algorithm:
F(k+1) = F(k) + sum(nums) - n * nums[n-k-1]
.Time Complexity: O(n) - We iterate through the array once to calculate F(0) and then n-1 times to compute subsequent F(k) values.
Space Complexity: O(1) - We use only a few variables to store the intermediate results.
The Python code efficiently implements this algorithm:
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
f = sum(i * v for i, v in enumerate(nums)) #calculate F(0)
n, s = len(nums), sum(nums) #store length and sum
ans = f # initialize max value
for i in range(1, n): #iterate to calculate remaining F(k)
f = f + s - n * nums[n - i] #apply the formula
ans = max(ans, f) # update max value if needed
return ans
The code in other languages (Java, C++, Go, Typescript, Rust) follows the same logic, adapting the syntax and data structures accordingly. The core algorithmic steps remain consistent across all implementations.