Rearrange an array to maximize i * arr [ i ] | Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver Code
function findOptimalSolution ( a , N ) { a . sort ( function ( a , b ) { return a - b } ) ; let points = 0 ; for ( let i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } let a = [ 1 , 4 , 2 , 3 , 9 ] ; let N = a . length ; document . write ( findOptimalSolution ( a , N ) ) ;