Rotate Function

MediumArrayMathDynamic ProgrammingBit Manipulation

Description

You are given an integer array nums of length n. Assume arrₖ is the array obtained by rotating nums by k positions clockwise (right). Define F(k) = 0 * arrₖ[0] + 1 * arrₖ[1] + … + (n - 1) * arrₖ[n - 1]. Return the maximum value of F(k) for 0 ≤ k < n. The answer fits in a 32-bit signed integer.

Examples

Input:nums = [4,3,2,6]
Output:26
Explanation:

F(0)=25, F(1)=16, F(2)=23, F(3)=26. Max = 26.

Input:nums = [100]
Output:0
Explanation:

Only F(0) = 0 * 100 = 0.

Input:nums = [1,2,3,4]
Output:20
Explanation:

F(0)=0+2+6+12=20 is the maximum.

Input:nums = [0,0,0,0]
Output:0
Explanation:

All rotations give 0.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 10⁵
  • -100 ≤ nums[i] ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!