Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed

Given an array arr[] of size N, the task is to find the maximum possible sum of i*arr[i] when the array can be rotated any number of times.
Examples :Â Â
Input: arr[] = {1, 20, 2, 10}
Output: 72.We can get 72 by rotating array twice.
{2, 10, 1, 20}
20*3 + 1*2 + 10*1 + 2*0 = 72Input: arr[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Output: 330
We can get 330 by rotating array 9 times.
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
0*1 + 1*2 + 2*3 … 9*10 = 330
Naive Approach:Â The basic idea of this approach isÂ
Find all rotations one by one, check the sum of every rotation and return the maximum sum.Â
Algorithm
- Start by initializing max_sum to INT_MIN
- Loop say i,from 0 to n-1:
- a. Initialize sum to 0
- b. Loop j from 0 to n-1:
- i. Calculate the index of the j-th element after rotation: (i+j) % n
- ii. Add the product of the element and its index to sum: j * arr[(i+j) % n]
- c. If sum is greater than max_sum, update max_sum to sum
- Return max_sum
C++
#include <climits>#include <iostream>Â
using namespace std;Â
int max_sum_rotation(int arr[], int n){    int max_sum = INT_MIN; // set the maximum sum to the                           // minimum possible value    for (int i = 0; i < n;         i++) { // loop through all possible rotations        int sum = 0; // set the current sum to zero        for (int j = 0; j < n;             j++) { // loop through all elements in the                    // array            int index                = (i + j)                  % n; // calculate the index of the current                       // element after rotation            sum += j * arr[index]; // add the product of the                                   // element with its index                                   // to the sum        }        max_sum = max(            max_sum,            sum); // update the maximum sum if necessary    }    return max_sum; // return the maximum sum obtained over                    // all rotations}Â
int main(){    int arr[] = {        10, 1, 2, 3, 4, 5, 6, 7, 8, 9    }; // define an array    int n = sizeof(arr)            / sizeof(                arr[0]); // calculate the size of the array    cout << max_sum_rotation(arr, n)         << endl; // call the function and print the result    return 0; // indicate successful program completion} |
Java
/*package whatever //do not write package name here */Â
import java.io.*;public class MaxSumRotation {    public static int maxSumRotation(int[] arr, int n) {        // Set the maximum sum to the minimum possible value        int maxSum = Integer.MIN_VALUE;Â
        // Loop through all possible rotations        for (int i = 0; i < n; i++) {            // Set the current sum to zero            int sum = 0;Â
            // Loop through all elements in the array            for (int j = 0; j < n; j++) {                // Calculate the index of the current element after rotation                int index = (i + j) % n;Â
                // Add the product of the element with its index to the sum                sum += j * arr[index];            }Â
            // Update the maximum sum if necessary            maxSum = Math.max(maxSum, sum);        }Â
        // Return the maximum sum obtained over all rotations        return maxSum;    }Â
    public static void main(String[] args) {        int[] arr = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};        int n = arr.length;        System.out.println(maxSumRotation(arr, n));    }} |
Python3
def max_sum_rotation(arr, n):    # set the maximum sum to the    # minimum possible value    max_sum = float('-inf')              # loop through all possible rotations    for i in range(n):               # set the current sum to zero        sum = 0                 # loop through all elements in the array        for j in range(n):            # calculate the index of the current element after rotation            index = (i + j) % n                         # add the product of the element with its index to the sum            sum += j * arr[index]                 # update the maximum sum if necessary        max_sum = max(max_sum, sum)         # return the maximum sum obtained over all rotations    return max_sum# Test casearr = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]n = len(arr)print(max_sum_rotation(arr, n)) |
Javascript
function maxSumRotation(arr) {Â Â Â Â const n = arr.length;Â Â Â Â let maxSum = Number.MIN_SAFE_INTEGER;Â
    for (let i = 0; i < n; i++) {        let sum = 0;Â
        for (let j = 0; j < n; j++) {            const index = (i + j) % n;            sum += j * arr[index];        }Â
        maxSum = Math.max(maxSum, sum);    }    return maxSum;}const arr = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9];console.log(maxSumRotation(arr)); |
330
Time Complexity: O(N2), where n is the size of the input array.Â
Auxiliary Space: O(1), because it uses a constant amount of extra space to store the variables max_sum, sum, i, and j.Â
Efficient Approach: The idea is as follows:
Let Rj be value of i*arr[i] with j rotations.Â
- The idea is to calculate the next rotation value from the previous rotation, i.e., calculate Rj from Rj-1.Â
- We can calculate the initial value of the result as R0, then keep calculating the next rotation values.Â
How to efficiently calculate Rj from Rj-1?Â
This can be done in O(1) time. Below are the details.Â
Let us calculate initial value of i*arr[i] with no rotation
R0 = 0*arr[0] + 1*arr[1] +…+ (n-1)*arr[n-1]After 1 rotation arr[n-1], becomes first element of array,Â
- arr[0] becomes second element, arr[1] becomes third element and so on.
- R1 = 0*arr[n-1] + 1*arr[0] +…+ (n-1)*arr[n-2]
- R1 – R0 = arr[0] + arr[1] + … + arr[n-2] – (n-1)*arr[n-1]
After 2 rotations arr[n-2], becomes first element of array,Â
- arr[n-1] becomes second element, arr[0] becomes third element and so on.
- R2 = 0*arr[n-2] + 1*arr[n-1] +…+ (n-1)*arr[n-3]
- R2 – R1 = arr[0] + arr[1] + … + arr[n-3] – (n-1)*arr[n-2] + arr[n-1]
If we take a closer look at above values, we can observe below pattern
Rj – Rj-1 = arrSum – n * arr[n-j],
Where arrSum is sum of all array elements, i.e., arrSum = ∑ arr[i] , 0 ≤ i ≤ N-1
Follow the below illustration for a better understanding.Â
Illustration:
Given arr[]={10, 1, 2, 3, 4, 5, 6, 7, 8, 9},Â
arrSum = 55, currVal = summation of (i*arr[i]) = Â 285
In each iteration the currVal is currVal = currVal + arrSum-n*arr[n-j] ,1st rotation: currVal = 285 + 55 –  (10 *  9) =  250
2nd rotation: currVal = 285 + 55 – (10 * 8) = 260
3rd rotation: currVal = 285 + 55 – (10 * 7) = 270
.
.
.
Last rotation: currVal = 285 + 55 – (10 * 1) = 330Previous currVal was 285, now it becomes 330.
It’s the maximum value we can find hence return 330.
Follow the steps mentioned below to implement the above approach:
- Compute the sum of all array elements. Let this sum be ‘arrSum‘.
- Compute R0 for the given array. Let this value be currVal.
- Loop from j = 1 to N-1 to calculate the value for each rotation:
- Update the currVal using the formula mentioned above.
- Update the maximum sum accordingly in each step.
- Return the maximum value as the required answer.
Below is the implementation of the above idea.
C++
// C++ program to find max value of i*arr[i]#include <iostream>using namespace std;Â
// Returns max possible value of i*arr[i]int maxSum(int arr[], int n){    // Find array sum and i*arr[i] with no rotation    int arrSum = 0; // Stores sum of arr[i]    int currVal = 0; // Stores sum of i*arr[i]    for (int i = 0; i < n; i++) {        arrSum = arrSum + arr[i];        currVal = currVal + (i * arr[i]);    }Â
    // Initialize result as 0 rotation sum    int maxVal = currVal;Â
    // Try all rotations one by one and find    // the maximum rotation sum.    for (int j = 1; j < n; j++) {        currVal = currVal + arrSum - n * arr[n - j];        if (currVal > maxVal)            maxVal = currVal;    }Â
    // Return result    return maxVal;}Â
// Driver programint main(void){Â Â Â Â int arr[] = { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â Â Â Â cout << "\nMax sum is " << maxSum(arr, n);Â Â Â Â return 0;} |
Java
// Java program to find max value of i*arr[i]Â
import java.util.Arrays;Â
class Test {Â Â Â Â static int arr[]Â Â Â Â Â Â Â Â = new int[] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 };Â
    // Returns max possible value of i*arr[i]    static int maxSum()    {        // Find array sum and i*arr[i] with no rotation        int arrSum = 0; // Stores sum of arr[i]        int currVal = 0; // Stores sum of i*arr[i]        for (int i = 0; i < arr.length; i++) {            arrSum = arrSum + arr[i];            currVal = currVal + (i * arr[i]);        }Â
        // Initialize result as 0 rotation sum        int maxVal = currVal;Â
        // Try all rotations one by one and find        // the maximum rotation sum.        for (int j = 1; j < arr.length; j++) {            currVal = currVal + arrSum                      - arr.length * arr[arr.length - j];            if (currVal > maxVal)                maxVal = currVal;        }Â
        // Return result        return maxVal;    }Â
    // Driver method to test the above function    public static void main(String[] args)    {        System.out.println("Max sum is " + maxSum());    }} |
Python
'''Python program to find maximum value of Sum(i*arr[i])'''Â
# returns max possible value of Sum(i*arr[i])Â
Â
def maxSum(arr):Â
    # stores sum of arr[i]    arrSum = 0Â
    # stores sum of i*arr[i]    currVal = 0Â
    n = len(arr)Â
    for i in range(0, n):        arrSum = arrSum + arr[i]        currVal = currVal + (i*arr[i])Â
    # initialize result    maxVal = currValÂ
    # try all rotations one by one and find the maximum    # rotation sum    for j in range(1, n):        currVal = currVal + arrSum-n*arr[n-j]        if currVal > maxVal:            maxVal = currValÂ
    # return result    return maxValÂ
Â
# test maxsum(arr) functionif __name__ == '__main__':Â Â Â Â arr = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]Â Â Â Â print "Max sum is: ", maxSum(arr) |
C#
// C# program to find max value of i*arr[i]using System;Â
class Test {    static int[] arr        = new int[] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 };Â
    // Returns max possible value of i*arr[i]    static int maxSum()    {        // Find array sum and i*arr[i]        // with no rotation        int arrSum = 0; // Stores sum of arr[i]        int currVal = 0; // Stores sum of i*arr[i]Â
        for (int i = 0; i < arr.Length; i++) {            arrSum = arrSum + arr[i];            currVal = currVal + (i * arr[i]);        }Â
        // Initialize result as 0 rotation sum        int maxVal = currVal;Â
        // Try all rotations one by one and find        // the maximum rotation sum.        for (int j = 1; j < arr.Length; j++) {            currVal = currVal + arrSum                      - arr.Length * arr[arr.Length - j];            if (currVal > maxVal)                maxVal = currVal;        }Â
        // Return result        return maxVal;    }Â
    // Driver Code    public static void Main()    {        Console.WriteLine("Max sum is " + maxSum());    }}Â
// This article is contributed by vt_m. |
Javascript
<script>// JavaScript program to find max value of i*arr[i]Â
// Returns max possible value of i*arr[i]function maxSum(arr, n){    // Find array sum and i*arr[i] with no rotation    let arrSum = 0; // Stores sum of arr[i]    let currVal = 0; // Stores sum of i*arr[i]    for (let i=0; i<n; i++)    {        arrSum = arrSum + arr[i];        currVal = currVal+(i*arr[i]);    }Â
    // Initialize result as 0 rotation sum    let maxVal = currVal;Â
    // Try all rotations one by one and find    // the maximum rotation sum.    for (let j=1; j<n; j++)    {        currVal = currVal + arrSum-n*arr[n-j];        if (currVal > maxVal)            maxVal = currVal;    }Â
    // Return result    return maxVal;}Â
// Driver program    let arr = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9];    let n = arr.length;    document.write("Max sum is " + maxSum(arr, n));Â
Â
Â
Â
Â
Â
// This code is contributed by Surbhi Tyagi.</script> |
PHP
<?php// PHP program to find max // value of i*arr[i] Â
// Returns max possible // value of i*arr[i]function maxSum($arr, $n){    // Find array sum and    // i*arr[i] with no rotation         // Stores sum of arr[i]    $arrSum = 0;          // Stores sum of i*arr[i]    $currVal = 0;     for ($i = 0; $i < $n; $i++)    {        $arrSum = $arrSum + $arr[$i];        $currVal = $currVal +                   ($i * $arr[$i]);    }Â
    // Initialize result as    // 0 rotation sum    $maxVal = $currVal;Â
    // Try all rotations one     // by one and find the     // maximum rotation sum.    for ($j = 1; $j < $n; $j++)    {        $currVal = $currVal + $arrSum -                    $n * $arr[$n - $j];        if ($currVal > $maxVal)            $maxVal = $currVal;    }Â
    // Return result    return $maxVal;}Â
// Driver Code$arr = array (10, 1, 2, 3, 4,              5, 6, 7, 8, 9);$n = sizeof($arr);echo "Max sum is " ,     maxSum($arr, $n);Â
// This code is contributed by m_kit?> |
Max sum is 330
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



