
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed in C++
- JavaScript Program for Maximum Sum of i*arr[i] Among all Rotations of a Given Array
- Maximum sum of i * arr[i] among all rotations of a given array in C++
- Maximum value of arr[i] % arr[j] for a given array 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++
- Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++
- Maximum modulo of all the pairs of array where arr[i] >= arr[j] in C++
- Rearrange array such that arr[i] >= arr[j] if i is even and arr[i]
- Maximize the sum of arr[i]*i in C++
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Maximum sum of pairwise product in an array with negative allowed in C++ program
- C++ program to find out the maximum value of i
- Sum of the elements from index L to R in an array when arr[i] = i * (-1)^i in C++
- Rearrange an Array to Maximize i*arr[i] using C++
