JavaScript Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed


We will be using a mathematical approach to find the maximum value of the sum of the product of the index and the value of the elements in the array. By rotating the array, we can maximize this sum by placing the maximum value of the array at the index with the maximum product. The algorithm we will be using involves finding the sum of the products of the index and values of the elements, then adding the difference between this sum and the product of the length of the array and the sum of the index values to this sum.

In the future, we will be continuously applying this algorithm to different arrays to find the maximum value of the sum of the products of the index and values of the elements with only rotations allowed. This solution is efficient as it only requires a single pass through the array and has a time complexity of O(n). By using this algorithm, we can quickly and easily find the maximum sum of the products of the index and values of the elements in an array.

Approach

  • The sum of all rotations can be obtained by multiplying each element in the array by its corresponding index and summing up the result.

  • The maximum value can be obtained by finding the index with the maximum value and rotating the array so that the maximum value becomes the first element.

  • The maximum value can be found by summing up the values of each element multiplied by its index and comparing it with the current maximum value.

  • The sum of all rotations can be found by adding the sum of all rotations to the current sum and dividing by the number of rotations.

  • The maximum value can be returned as the result.

Example

The problem can be solved by first finding the sum of all elements in the array and then iteratively rotating the array and updating the sum by adding the difference of current rotation to the previous sum. The maximum sum would be the answer. Here is a complete example in JavaScript −

function maxSum(arr) {
   let n = arr.length;
   let arrSum = 0;
   let currVal = 0;
   for (let i = 0; i < n; i++) {
      arrSum += arr[i];
      currVal += i * arr[i];
   }
   let maxVal = currVal;
   for (let j = 1; j < n; j++) {
      currVal = currVal + arrSum - n * arr[n - j];
      maxVal = Math.max(maxVal, currVal);
   }
   return maxVal;
}
let arr = [1, 20, 2, 10];
console.log(maxSum(arr)); // Output: 72

Explanation

  • The function maxSum takes an array as input and returns the maximum sum that can be obtained by rotating the array and taking the sum of i * arr[i] for each rotation.

  • The variable n stores the length of the array.

  • The variable arrSum stores the sum of all elements in the array and is initialized to 0.

  • The variable currVal stores the sum of i * arr[i] for the current rotation and is initialized to 0.

  • The first loop calculates the sum of all elements in the array and the sum of i * arr[i] for the first rotation.

  • The variable maxVal stores the maximum sum and is initialized to currVal.

  • The second loop iteratively rotates the array and updates the sum of i * arr[i] for each rotation. The sum of i * arr[i] for the current rotation is updated by adding the difference of current rotation to the previous sum.

  • The value of currVal is updated by adding the difference between the sum of i * arr[i] for the current rotation and the sum of i * arr[i] for the previous rotation. The difference is calculated by subtracting n * arr[n - j] from arrSum.

  • The maximum value of currVal for each rotation is stored in maxVal using Math.max function.

  • Finally, the value of maxVal is returned as the answer.

Updated on: 15-Mar-2023

214 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements