Maximum sum of i * arr[i] among all rotations of a given array in C++


In this problem, we are given an array arr. Our task is to create a program that will find the maximum sum of i*arr[i] among all rotations of the given array in C++.

Program description − Here, we will find the maximum sum of out the sums of all the elements of the array multiplied by their index {i * arr[i]} in the rotation.

Let’s take an example to understand the problem,

Input − array arr = {4, 8, 1, 5}

Output − 37

Explanation

All rotations with the sum of i*arr[i] :
{4, 8, 1, 5} = 4*0 + 8*1 + 1*2 + 5*3 = 25
{8, 1, 5, 4} = 8*0 + 1*1 + 5*2 + 4*3 = 23
{1, 5, 4, 8} = 1*0 + 5*1 + 4*2 + 8*3 = 37
{5, 4, 8, 1} = 5*0 + 4*1 + 8*2 + 1*3 = 23
The max sum of i*arr[i] is for third rotation.

A simple solution to this problem is to calculate the sum of all elements multiplied by its index of each rotation. And then find the maximum of the sums of all the rotations. For this, we will rotate the array n times and calculate the sums for each and store the sum of a maxSum variable if the sum of current rotation is greater than the last.

Example

Program to show the implementation of this solution,

 Live Demo

#include<iostream>
using namespace std;
int findMax(int a, int b){
   if(a>b)
      return a;
return b;
}
int calculateMaxSum(int arr[], int n){
   int maxSum = 0, sum = 0;
   for (int i=0; i<n; i++){
      sum = 0;
      for (int j=0; j<n; j++){
         int index = (i+j)%n;
         sum += j*arr[index];
      }
      maxSum = findMax(maxSum, sum);
   }
   return maxSum;
}
int main(){
   int arr[] = {4, 8, 1, 5};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout<<"The maximum sum of all the rotation of the array is "<<calculateMaxSum(arr, n);
   return 0;
}

Output

The maximum sum of all the rotation of the array is 37

An efficient solution is to use the calculate the sum of the next rotation using previous rotation. We will use the formula,

nextSum = currentSum - (arraySum - arr[i-1]) + arr[i-1]*(n-1)

Using this formula, we will find nextSum and at the end of the loop body, we will check if nextSum is greater the maxSum , if yes then maxSum = nextSum.

Example

Program to illustrate the working of this solution,

 Live Demo

#include<iostream>
using namespace std;
int findMax(int a, int b){
   if(a > b)
      return a;
   return b;
}
int calculateMaxSum(int arr[], int n){
   int arraySum = 0, currentSum = 0, nextSum ;
   for (int i=0; i<n; i++){
      arraySum += arr[i];
      currentSum += i*arr[i];
   }
   int maxSum = currentSum;
   for (int i=1; i<n; i++){
      nextSum = currentSum - (arraySum - arr[i-1]) + arr[i-1] * (n1);
      currentSum = nextSum;
      maxSum = findMax(maxSum, nextSum);
   }
   return maxSum;
}
int main(){
   int arr[] = {4, 8, 1, 5};
   int n = sizeof(arr)/sizeof(arr[0]);
   cout<<"The maximum sum of all the rotation of the array is "<<calculateMaxSum(arr, n);
   return 0;
}

Output

The maximum sum of all the rotation of the array is 37

Updated on: 03-Jun-2020

376 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements