Python Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
Last Updated :
28 Dec, 2021
Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i].
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 = 72
Input: 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
We strongly recommend you to minimize your browser and try this yourself first.
A Simple Solution is to find all rotations one by one, check sum of every rotation and return the maximum sum. Time complexity of this solution is O(n2).
We can solve this problem in O(n) time using an Efficient Solution.
Let Rj be value of i*arr[i] with j rotations. The idea is to calculate next rotation value from previous rotation, i.e., calculate Rj from Rj-1. We can calculate initial value of result as R0, then keep calculating next rotation values.
How to efficiently calculate Rj from Rj-1?
This can be done in O(1) time. Below are 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
Below is complete algorithm:
1) Compute sum of all array elements. Let this sum be 'arrSum'.
2) Compute R0 by doing i*arr[i] for given array.
Let this value be currVal.
3) Initialize result: maxVal = currVal // maxVal is result.
// This loop computes Rj from Rj-1
4) Do following for j = 1 to n-1
......a) currVal = currVal + arrSum-n*arr[n-j];
......b) If (currVal > maxVal)
maxVal = currVal
5) Return maxVal
Below is the implementation of above idea :
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) function
arr = [10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print "Max sum is: ", maxSum(arr)
Output :
Max sum is 330
Time Complexity : O(n)
Auxiliary Space : O(1)
Please refer complete article on
Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed for more details!
Similar Reads
Python Program to Find the 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: 29Explanation: 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 =
5 min read
Python3 Program to Find Maximum value possible by rotating digits of a given number Given a positive integer N, the task is to find the maximum value among all the rotations of the digits of the integer N.Examples:Input: N = 657Output: 765Explanation: All rotations of 657 are {657, 576, 765}. The maximum value among all these rotations is 765.Input: N = 7092Output: 9270Explanation:
2 min read
Python3 Program to Modify given array to a non-decreasing array by rotation Given an array arr[] of size N (consisting of duplicates), the task is to check if the given array can be converted to a non-decreasing array by rotating it. If it's not possible to do so, then print "No". Otherwise, print "Yes".Examples:Input: arr[] = {3, 4, 5, 1, 2}Output: YesExplanation: After 2
3 min read
Python3 Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types:If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array.Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, Qu
4 min read
Python3 Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns Given a square matrix, mat[][] of dimensions N * N, the task is find the maximum sum of diagonal elements possible from the given matrix by rotating either all the rows or all the columns of the matrix by a positive integer.Examples:Input: mat[][] = { { 1, 1, 2 }, { 2, 1, 2 }, { 1, 2, 2 } }Output: 6
3 min read