Open in App
Not now

# Maximise product of each array element with their indices by rearrangement

• Difficulty Level : Easy
• Last Updated : 17 Aug, 2022

Given an Array of N integers, the task is to maximize the value of the product of each array element with their corresponding indices by rearranging the array.

Examples:

Input: N = 4, arr[] = { 3, 5, 6, 1 }
Output: 31
Explanation: If we arrange arr[] as { 1, 3, 5, 6 }. Sum of arr[i]*i is 1*0 + 3*1 + 5*2 + 6*3 = 31, which is maximum

Input: N = 2, arr[] = { 19, 20 }
Output: 20

## Maximise product of each array element with their indices by rearrangement using Sorting

The idea is based on the fact that the largest value should be scaled maximum and the smallest value should be scaled minimum. So we multiply the minimum value of i with the minimum value of arr[i]. So, sort the given array in increasing order and compute the sum of arr[i]*i.

Follow the steps mentioned below to implement the idea:

• Sort the array.
• Create a variable sum to store the final answer
• Traverse The array, while traversing the array increases the value of the sum by arr[I]*i.
• Return sum.

Below is the implementation of the above approach:

## C++

 `// CPP program to find the maximum value` `// of i*arr[i]` `#include ` `using` `namespace` `std;`   `int` `maxSum(``int` `arr[], ``int` `n)` `{` `    ``// Sort the array` `    ``sort(arr, arr + n);`   `    ``// Finding the sum of arr[i]*i` `    ``int` `sum = 0;` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``sum += (arr[i] * i);`   `    ``return` `sum;` `}`   `// Driven Program` `int` `main()` `{` `    ``int` `arr[] = { 3, 5, 6, 1 };` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``cout << maxSum(arr, n) << endl;` `    ``return` `0;` `}`

## Java

 `// Java program to find the` `// maximum value of i*arr[i]` `import` `java.util.*;`   `class` `GFG {`   `    ``static` `int` `maxSum(``int` `arr[], ``int` `n)` `    ``{` `        ``// Sort the array` `        ``Arrays.sort(arr);`   `        ``// Finding the sum of arr[i]*i` `        ``int` `sum = ``0``;` `        ``for` `(``int` `i = ``0``; i < n; i++)` `            ``sum += (arr[i] * i);`   `        ``return` `sum;` `    ``}`   `    ``// Driven Program` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `arr[] = { ``3``, ``5``, ``6``, ``1` `};` `        ``int` `n = arr.length;`   `        ``System.out.println(maxSum(arr, n));` `    ``}` `}` `// This code is contributed by Prerna Saini`

## Python3

 `# Python program to find the` `# maximum value of i*arr[i]`     `def` `maxSum(arr, n):`   `    ``#  Sort the array` `    ``arr.sort()`   `    ``# Finding the sum of` `    ``# arr[i]*i` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(n):` `        ``sum` `+``=` `arr[i] ``*` `i`   `    ``return` `sum`     `# Driver Program` `arr ``=` `[``3``, ``5``, ``6``, ``1``]` `n ``=` `len``(arr)` `print``(maxSum(arr, n))`   `# This code is contributed` `# by Shrikant13`

## C#

 `// C# program to find the` `// maximum value of i*arr[i]` `using` `System;`   `class` `GFG {`   `    ``// Function to find the` `    ``// maximum value of i*arr[i]` `    ``static` `int` `maxSum(``int``[] arr, ``int` `n)` `    ``{`   `        ``// Sort the array` `        ``Array.Sort(arr);`   `        ``// Finding the sum of arr[i]*i` `        ``int` `sum = 0;` `        ``for` `(``int` `i = 0; i < n; i++)` `            ``sum += (arr[i] * i);`   `        ``return` `sum;` `    ``}`   `    ``// Driver code` `    ``static` `public` `void` `Main()` `    ``{` `        ``int``[] arr = { 3, 5, 6, 1 };` `        ``int` `n = arr.Length;`   `        ``Console.WriteLine(maxSum(arr, n));` `    ``}` `}`   `// This code is contributed by Ajit.`

## PHP

 ``

## Javascript

 ``

Output

`31`

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles