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

• Difficulty Level : Easy
• Last Updated : 11 May, 2021

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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

 ``

## Javascript

 ``
Output:
`56`

My Personal Notes arrow_drop_up