# Rearrange an array to maximize i*arr[i]

Given an array of N integers, you have to select all of these integers in any order. For every integer you select, you get points equal to the value of: the selected integer * number of integers selected before the current integer. Your task is to maximize these points.
Note: You can select every integer exactly 1 time.

Examples:

Input: a = {1, 4, 2, 3, 9}
Output: 56
The optimal solution for this array would be select the numbers in the order 1, 2, 3, 4, 9 that gives points 0, 2, 6, 12, 36 respectively and giving a total maximum points of 56.

Input: a = {1, 2, 2, 4, 9}
Output: 54
The optimal solution for this array would be select the numbers in the order 1, 2, 2, 4, 9 that gives points 0, 2, 4, 12, 36 respectively and giving a total maximum points of 54.

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

The idea is to use Greedy Approach, i.e., maximize multiplier for the largest element.
We sort the given array and start picking elements in this sorted manner, starting with the first element.

Below is the implementation of the above approach:

## C++

 `// C++ program for the Optimal Solution ` `#include   ` `#include ` `using` `namespace` `std; ` ` `  `    ``// Function to calculate the maximum points ` `    ``// earned by making an optimal selection on ` `    ``// the given array ` `    ``static` `int` `findOptimalSolution(``int` `a[], ``int` `N) ` `    ``{ ` `        ``// Sorting the array ` `        ``sort(a, a+N);  ` `        ``// Variable to store the total points earned ` `        ``int` `points = 0; ` ` `  `        ``for` `(``int` `i = 0; i < N; i++) { ` `            ``points += a[i] * i; ` `        ``} ` `        ``return` `points; ` `    ``} ` ` `  `    ``// Driver code ` `int` `main() { ` `    ``int` `a[] = { 1, 4, 2, 3, 9 }; ` `    ``int` `N = ``sizeof``(a)/``sizeof``(a); ` `        ``cout<<(findOptimalSolution(a, N)); ` `     `  `    ``return` `0; ` `} `

## Java

 `// Java program for the Optimal Solution ` `import` `java.io.*; ` `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Function to calculate the maximum points ` `    ``// earned by making an optimal selection on ` `    ``// the given array ` `    ``static` `int` `findOptimalSolution(``int``[] a, ``int` `N) ` `    ``{ ` `        ``// Sorting the array ` `        ``Arrays.sort(a); ` ` `  `        ``// Variable to store the total points earned ` `        ``int` `points = ``0``; ` ` `  `        ``for` `(``int` `i = ``0``; i < N; i++) { ` `            ``points += a[i] * i; ` `        ``} ` `        ``return` `points; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int``[] a = { ``1``, ``4``, ``2``, ``3``, ``9` `}; ` `        ``int` `N = a.length; ` `        ``System.out.println(findOptimalSolution(a, N)); ` `    ``} ` `} `

## Python3

 `# Python3 program for the Optimal Solution  ` ` `  `# Function to calculate the maximum points  ` `# earned by making an optimal selection on  ` `# the given array  ` `def` `findOptimalSolution(a, N) : ` `         `  `    ``# Sorting the array  ` `    ``a.sort()   ` `     `  `    ``# Variable to store the total points earned  ` `    ``points ``=` `0`  `   `  `    ``for` `i ``in` `range``(``0``, N): ` `        ``points ``+``=` `a[i] ``*` `i  ` `      `  `    ``return` `points  ` `   `  `     `  `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``a ``=` `[``1``, ``4``, ``2``, ``3``, ``9``]  ` `    ``N ``=` `len``(a)  ` `    ``print``(findOptimalSolution(a, N))  `

## C#

 `//C# program for the Optimal Solution ` `using` `System; ` ` `  `public` `class` `GFG{ ` `     `  `        ``// Function to calculate the maximum points  ` `    ``// earned by making an optimal selection on  ` `    ``// the given array  ` `    ``static` `int` `findOptimalSolution(``int``[]a, ``int` `N)  ` `    ``{  ` `        ``// Sorting the array  ` `        ``Array.Sort(a);  ` ` `  `        ``// Variable to store the total points earned  ` `        ``int` `points = 0;  ` ` `  `        ``for` `(``int` `i = 0; i < N; i++) {  ` `            ``points += a[i] * i;  ` `        ``}  ` `        ``return` `points;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``static` `public` `void` `Main (){ ` `        ``int``[] a = { 1, 4, 2, 3, 9 };  ` `        ``int` `N = a.Length;  ` `        ``Console.WriteLine(findOptimalSolution(a, N));  ` `    ``} ` `//This code is contributed by ajit     ` `} `

## PHP

 ` `

Output:

```56
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : jit_t, rituraj_jain