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.length1 <= n <= 105-100 <= nums[i] <= 100Problem Overview: Given an integer array nums, define a rotation function F(k) where the array is rotated k positions and each element is multiplied by its index. The goal is to compute the maximum value among all rotations.
Approach 1: Brute Force Rotation Simulation (Time: O(n^2), Space: O(1))
Directly simulate every rotation and compute the function value from scratch. For each rotation k, iterate through the array and calculate F(k) = sum(i * rotated[i]). Rotating or indexing elements for every k requires scanning the entire array, which results in O(n) work per rotation and n total rotations. The approach is straightforward and useful for validating correctness, but it becomes slow when n grows because the total runtime reaches O(n^2). This method mainly relies on basic array traversal.
Approach 2: Mathematical Recurrence Optimization (Time: O(n), Space: O(1))
The key observation is that consecutive rotation values are related. If F(k) is known, the next rotation can be derived using a recurrence instead of recomputing everything. Let S be the total sum of all elements. After rotating once, each element’s index increases by 1 except the last element which moves to index 0. This leads to the formula F(k) = F(k-1) + S - n * nums[n-k]. Start by computing F(0) and the total sum in one pass. Then iterate through rotations using the recurrence to update the value in constant time. This transforms the problem into a linear scan using ideas from math and recurrence-style reasoning similar to dynamic programming. The algorithm evaluates all rotations in O(n) time while using only constant extra space.
Recommended for interviews: The mathematical recurrence approach is the expected solution. Interviewers often want to see whether you recognize the relationship between consecutive rotations and avoid recomputing the weighted sum each time. Implementing the brute force version first demonstrates understanding of the rotation function, but deriving the recurrence and reducing the complexity to O(n) shows strong problem‑solving skills.
The brute force approach involves recalculating the rotation function F(k) for each rotation k from 0 to n-1. For each k, simulate the rotation and compute the result using the definition provided. This, however, can be time-consuming for larger arrays due to recalculating rotations or the function from scratch each time.
This code iterates through each rotation k, calculating the rotation function F(k) by summing the product of index and the rotated values. It checks if the current result is higher than the maximum found so far.
Time Complexity: O(n^2), where n is the length of nums.
Space Complexity: O(1), no additional space beyond input storage.
This optimized approach utilizes a mathematical relationship between F(k) and F(k+1). Notice how the array rotates and calculate the sum considering the differences. Thereby avoiding recalculation of entire sets of sums, utilizing already computed values dynamically.
Calculate the initial F(0) and keep updating F based on the sum formula: F(k) = F(k-1) + sum - n * nums[n-k], where sum is the sum of all elements. Store the maximum value as you compute.
Time Complexity: O(n), where n is the length of numbers.
Space Complexity: O(1), using constant extra space.
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2), where n is the length of nums. |
| Optimized Approach Using Mathematical Formula | Time Complexity: O(n), where n is the length of numbers. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Rotation Simulation | O(n^2) | O(1) | Useful for understanding the definition of the rotation function or verifying correctness on small inputs |
| Mathematical Recurrence Optimization | O(n) | O(1) | Best choice for interviews and large arrays since each rotation value is derived in constant time |
Leetcode 396. Rotate Function • CodeItUp • 2,611 views views
Watch 9 more video solutions →Practice Rotate Function with our built-in code editor and test cases.
Practice on FleetCode