Rearrange an array to maximize i * arr [ i ] | C ++ program for the Optimal Solution ; 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
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; static int findOptimalSolution ( int a [ ] , int N ) { sort ( a , a + N ) ; int points = 0 ; for ( int i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } int main ( ) { int a [ ] = { 1 , 4 , 2 , 3 , 9 } ; int N = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << ( findOptimalSolution ( a , N ) ) ; return 0 ; }