
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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,
#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,
#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
- Related Articles
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++
- JavaScript Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- Maximum value of arr[i] % arr[j] for a given array in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Find Maximum value of abs(i – j) * min(arr[i], arr[j]) in an array arr[] in C++
- Maximum value of |arr[i] – arr[j] - + |i – j| in C++
- Maximum sum of non-leaf nodes among all levels of the given binary tree in C++
- Maximize the sum of arr[i]*i in C++
- Maximum value of XOR among all triplets of an array in C++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Find the sum of maximum difference possible from all subset of a given array in Python
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j] in C++
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++
