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
< ? php function findOptimalSolution ( $ a , $ N ) { sort ( $ a ) ; $ points = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ points += $ a [ $ i ] * $ i ; } return $ points ; } $ a = array ( 1 , 4 , 2 , 3 , 9 ) ; $ N = sizeof ( $ a ) ; echo ( findOptimalSolution ( $ a , $ N ) ) ; ? >