Open In App

Javascript Program for Maximum sum of i*arr[i] among all rotations of a given array

Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1.

Examples:  



Input: arr[] = {8, 3, 1, 2}
Output: 29
Explanation: Lets look at all the rotations,
{8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11
{3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 8*3 = 29
{1, 2, 8, 3} = 1*0 + 2*1 + 8*2 + 3*3 = 27
{2, 8, 3, 1} = 2*0 + 8*1 + 3*2 + 1*3 = 17

Input: arr[] = {3, 2, 1}
Output: 7
Explanation: Lets look at all the rotations,
{3, 2, 1} = 3*0 + 2*1 + 1*2 = 4
{2, 1, 3} = 2*0 + 1*1 + 3*2 = 7
{1, 3, 2} = 1*0 + 3*1 + 2*2 = 7

Method 1: This method discusses the Naive Solution which takes O(n2) amount of time. 
The solution involves finding the sum of all the elements of the array in each rotation and then deciding the maximum summation value. 




<script>
// A Naive javascript program to find
// maximum sum rotation    // Returns maximum value of i*arr[i]
    function maxSum(arr , n) {
        // Initialize result
        var res = Number.MIN_VALUE;
 
        // Consider rotation beginning with i
        // for all possible values of i.
        for (i = 0; i < n; i++) {
 
            // Initialize sum of current rotation
            var curr_sum = 0;
 
            // Compute sum of all values. We don't
            // actually rotation the array, but compute
            // sum by finding ndexes when arr[i] is
            // first element
            for (j = 0; j < n; j++) {
                var index = (i + j) % n;
                curr_sum += j * arr[index];
            }
 
            // Update result if required
            res = Math.max(res, curr_sum);
        }
 
        return res;
    }
 
    // Driver code
     
        var arr = [ 8, 3, 1, 2 ];
        var n = arr.length;
        document.write(maxSum(arr, n));
 
// This code contributed by umadevi9616
</script>

Output :



29

Method 2: This method discusses the efficient solution which solves the problem in O(n) time. In the naive solution, the values were calculated for every rotation. So if that can be done in constant time then the complexity will decrease.

next_val = curr_val - (cum_sum - arr[i-1]) + arr[i-1] * (n-1);

next_val = Value of ∑i*arr[i] after one rotation.
curr_val = Current value of ∑i*arr[i] 
cum_sum = Sum of all array elements, i.e., ∑arr[i].

Lets take example {1, 2, 3}. Current value is 1*0+2*1+3*2
= 8. Shifting it by one will make it {2, 3, 1} and next value
will be 8 - (6 - 1) + 1*2 = 5 which is same as 2*0 + 3*1 + 1*2




<script>
// An efficient JavaScript program to compute
// maximum sum of i*arr[i]
 
function maxSum(arr, n)
{
    // Compute sum of all array elements
    let cum_sum = 0;
    for (let i=0; i<n; i++)
        cum_sum += arr[i];
 
    // Compute sum of i*arr[i] for initial
    // configuration.
    let curr_val = 0;
    for (let i=0; i<n; i++)
        curr_val += i*arr[i];
 
    // Initialize result
    let res = curr_val;
 
    // Compute values for other iterations
    for (let i=1; i<n; i++)
    {
        // Compute next value using previous
        // value in O(1) time
        let next_val = curr_val - (cum_sum - arr[i-1])
                    + arr[i-1] * (n-1);
 
        // Update current value
        curr_val = next_val;
 
        // Update result if required
        res = Math.max(res, next_val);
    }
 
    return res;
}
 
// Driver code
    let arr = [8, 3, 1, 2];
    let n = arr.length;
    document.write(maxSum(arr, n) + "<br>");
 
 
 
 
 
// This code is contributed by Surbhi Tyagi.
</script>

Output: 

29

Method 3: The method discusses the solution using pivot in O(n) time. The pivot method can only be used in the case of a sorted or a rotated sorted array. For example: {1, 2, 3, 4} or {2, 3, 4, 1}, {3, 4, 1, 2} etc.




<script>
 
// js program to find maximum sum
// of all rotation of i*arr[i] using pivot.
 
// function definition
function maxSum(arr, n)
{
    let sum = 0;
    let i;
    let pivot = findPivot(arr,n);
     
    // difference in pivot and index of
    // last element of array
    let diff = n - 1 - pivot;
    for (i = 0;i < n;i++)
    {
        sum = sum + ((i + diff) % n) * arr[i];
    }
     
    return sum;
 
}
 
// function to find pivot
function findPivot(arr, n)
{
    let i;
    for (i = 0; i < n; i++)
    {
        if (arr[i] > arr[(i + 1) % n])
        {
            return i;
        }
    }
    return 0;
}
 
// Driver code
 
// rotated input array
let arr = [8, 3, 1, 2];
let n = arr.length;
let ma = maxSum(arr,n);
document.write(ma);
 
// This code is contributed by mohit kumar 29.
</script>

Output: 

29

Article Tags :