Leetcode 396 Solution

This article provides solution to leetcode question 396 (rotate-function)

https://leetcode.com/problems/rotate-function

Solution

class Solution {
public:
    int maxRotateFunction(vector<int>& A) {
        if (A.size() == 0)
            return 0;

        int f = 0;
        int s = 0;
        int maxf = INT_MIN;

        for (int i = 0; i < A.size(); i++)
            f += i * A[i], s += A[i];

        for (int i = A.size() - 1; i >= 0; i--)
        {
            f += s - A.size() * A[i];

            maxf = max(f, maxf);
        }

        return maxf;
    }
};