PHP Program to Find Maximum Value of Sum ( i*arr[i]) with Only Rotations on Given Array Allowed
Last Updated :
22 Jul, 2024
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
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 :
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);
?>
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
Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed Given an array arr[], the task is to determine the maximum possible value of the expression i*arr[i] after rotating the array any number of times (including zero).Note: In each rotation, every element of the array shifts one position to the right, and the last element moves to the front.Examples : I
12 min read
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 +
7 min read
Maximum sum of i*arr[i] among all rotations of a given array Given an integer array arr[] of size n. The task is to find the maximum value of the sum of the value of i * arr[i] where i varies from 0 to n-1. The only operation allowed is to rotate the array any number of times.Examples: Input: arr[] = [8, 3, 1, 2]Output: 29Explanation: Out of all the possible
13 min read
How to Find Max Sum (i*arr[i]) with Array Rotations in JavaScript? Given an array where rotation operations are allowed, the task is to rotate the array any number of times and find the maximum possible sum of i*arr[i]. Examples : Input: arr = [1, 20, 2, 10] Output: 72 Explanation: We can get 72 by rotating array twice. [2, 10, 1, 20] 20*3 + 1*2 + 10*1 + 2*0 = 72 I
4 min read
Javascript 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,
5 min read
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,
13 min read
Minimum rotations that have maximum elements with value at most its index Given an array arr[] of size N, the array can be rotated any number of times such that after rotation, each element of the array arr[] that is less than or equal to its index will get 1 point. The task is to find the rotation index K that corresponds to the highest score. If there are multiple answe
11 min read
Javascript 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:
3 min read
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:
8 min read
Count of possible rotations of given Array to remove largest element from first half Given an array arr[ ] with even length N, the task is to find the number of cyclic shifts (rotations) possible for this array, such that the first half of array does not contain the maximum element. Examples: Input: N = 6, arr[ ] = { 3, 3, 5, 3, 3, 3 }Output: 3Explanation: The maximum element here i
6 min read