396. Rotate Function
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the maximum value of a "rotation function" F(k) for all possible clockwise rotations of an array nums. The function F(k) is a weighted sum of the elements of the rotated array arrk, where the weight of each element is its index. A naive approach of actually rotating the array for each k and then computing the sum would be O(n²), which is too slow given n can be up to 10⁵. The key to an efficient solution is to find a mathematical relationship between F(k) and F(k-1) to avoid redundant computations.
Solution
Rotate Function in Python
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
n = len(nums)
if n == 0:
return 0
# In Python, integers have arbitrary precision, so no overflow issues
s = sum(nums)
f0 = 0
for i, num in enumerate(nums):
f0 += i * num
max_val = f0
current_f = f0
# Iterate from k = 1 to n-1
for k in range(1, n):
# Use the recurrence relation: F(k) = F(k-1) + sum - n * nums[n-k]
# nums[n-k] is the element that was at the last position in the previous rotation
# In Python, nums[-k] is a convenient way to access nums[n-k]
current_f = current_f + s - n * nums[n - k]
if current_f > max_val:
max_val = current_f
return max_val
Rotate Function in Java
class Solution {
public int maxRotateFunction(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
// Use long for sum and function values to prevent overflow
long sum = 0;
for (int num : nums) {
sum += num;
}
long f0 = 0;
for (int i = 0; i < n; i++) {
f0 += (long)i * nums[i];
}
long maxVal = f0;
long currentF = f0;
// Iterate from k = 1 to n-1
for (int k = 1; k < n; k++) {
// Use the recurrence relation: F(k) = F(k-1) + sum - n * nums[n-k]
// nums[n-k] is the element that was at the last position in the previous rotation
currentF = currentF + sum - (long)n * nums[n - k];
if (currentF > maxVal) {
maxVal = currentF;
}
}
// The problem guarantees the answer fits in a 32-bit integer.
return (int)maxVal;
}
}
Rotate Function in C++
class Solution {
public:
int maxRotateFunction(vector<int>& nums) {
int n = nums.size();
if (n == 0) {
return 0;
}
// Use long long for sum and function values to prevent overflow
long long sum = 0;
for (int num : nums) {
sum += num;
}
long long f0 = 0;
for (int i = 0; i < n; ++i) {
f0 += (long long)i * nums[i];
}
long long max_val = f0;
long long current_f = f0;
// Iterate from k = 1 to n-1
for (int k = 1; k < n; ++k) {
// Use the recurrence relation: F(k) = F(k-1) + sum - n * nums[n-k]
// nums[n-k] is the element that was at the last position in the previous rotation
current_f = current_f + sum - (long long)n * nums[n - k];
if (current_f > max_val) {
max_val = current_f;
}
}
// The problem guarantees the answer fits in a 32-bit integer.
return (int)max_val;
}
};
Rotate Function in TypeScript
function maxRotateFunction(nums: number[]): number {
const n = nums.length;
if (n === 0) {
return 0;
}
// JavaScript's 'number' type is 64-bit float, safe for these calculations.
let sum = 0;
for (const num of nums) {
sum += num;
}
let f0 = 0;
for (let i = 0; i < n; i++) {
f0 += i * nums[i];
}
let maxVal = f0;
let currentF = f0;
// Iterate from k = 1 to n-1
for (let k = 1; k < n; k++) {
// Use the recurrence relation: F(k) = F(k-1) + sum - n * nums[n-k]
// nums[n-k] is the element that was at the last position in the previous rotation
currentF = currentF + sum - n * nums[n - k];
if (currentF > maxVal) {
maxVal = currentF;
}
}
return maxVal;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.