You are given an integer array nums of length n. Assume arr_k to be an array obtained by rotating nums by k positions clock-wise. We define the rotation function F on nums as: F(k) = 0 * arr_k[0] + 1 * arr_k[1] + ... + (n - 1) * arr_k[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 = 25. F(1) = 16, F(2) = 23 and F(3) = 0·3 + 1·2 + 2·6 + 3·4 = 26. The maximum is 26.
Example 2
Input: nums = [100]
Output: 0
Explanation: With a single element, every rotation computes 0 × 100 = 0.
Example 3
Input: nums = [1,2,3]
Output: 8
Explanation: F(0) = 0·1 + 1·2 + 2·3 = 8, while the other two rotations both give 5. The maximum is 8.
Don't recompute `F(k)` from scratch. Look at how it changes when we rotate one more step — every element's coefficient goes up by `1`, except the one element that wraps around to position `0`, which loses `n - 1` from its coefficient. That single insight collapses an `O(n²)` brute force into an `O(n)` sweep.
Run checks all cases above. Submit evaluates all test cases.