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
PHP Tutorial PHP is a widely used, open-source server-side scripting language primarily designed for web development. It is embedded directly into HTML and generates dynamic content on web pages, making it essential for building data-driven websites. It allows developers to handle database interactions, session
9 min read
Rotate an Array by d - Counterclockwise or Left Given an array of integers arr[] of size n, the task is to rotate the array elements to the left by d positions.Examples:Input: arr[] = {1, 2, 3, 4, 5, 6}, d = 2Output: {3, 4, 5, 6, 1, 2}Explanation: After first left rotation, arr[] becomes {2, 3, 4, 5, 6, 1} and after the second rotation, arr[] bec
15+ min read
Top 60+ PHP Interview Questions and Answers -2025 PHP is a popular server-side scripting language, widely known for its efficiency in web development and versatility across various platforms. PHP is extensively utilized by top companies such as Facebook, WordPress, Slack, Wikipedia, MailChimp, and many more due to its robust features and high perfo
15+ min read
PHP Introduction PHP stands for Hypertext Preprocessor. It is an open-source, widely used language for web development. Developers can create dynamic and interactive websites by embedding PHP code into HTML. PHP can handle data processing, session management, form handling, and database integration. The latest versi
8 min read
Search in a Sorted and Rotated Array Given a sorted and rotated array arr[] of n distinct elements, the task is to find the index of given key in the array. If the key is not present in the array, return -1. Examples: Input: arr[] = [5, 6, 7, 8, 9, 10, 1, 2, 3], key = 3Output: 8Explanation: 3 is present at index 8 in arr[].Input: arr[]
15+ min read
PHP Arrays Arrays are one of the most important data structures in PHP. They allow you to store multiple values in a single variable. PHP arrays can hold values of different types, such as strings, numbers, or even other arrays. Understanding how to use arrays in PHP is important for working with data efficien
5 min read
Version Control Systems Version Control Systems (VCS) are essential tools used in software development and collaborative projects to track and manage changes to code, documents, and other files. Whether you're working alone or as part of a team, version control helps ensure that your work is safe, organized, and easy to co
7 min read
PHP | Functions A function in PHP is a self-contained block of code that performs a specific task. It can accept inputs (parameters), execute a set of statements, and optionally return a value. PHP functions allow code reusability by encapsulating a block of code to perform specific tasks.Functions can accept param
8 min read
Check if Strings Are Rotations of Each Other Given two string s1 and s2 of same length, the task is to check whether s2 is a rotation of s1.Examples: Input: s1 = "abcd", s2 = "cdab"Output: trueExplanation: After 2 right rotations, s1 will become equal to s2.Input: s1 = "aab", s2 = "aba"Output: trueExplanation: After 1 left rotation, s1 will be
15+ min read
Difference between HTTP GET and POST Methods HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET
4 min read