Find the Maximum possible Sum for the given conditions | C ++ program to find the maximum possible Sum for the given conditions ; Function to find the maximum possible sum for the given conditions ; Sorting the array ; Variable to store the answer ; Iterating through the array ; If the value is greater than 0 ; If the value becomes 0 then break the loop because all the weights after this index will be 0 ; Print profit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - ( 1 * i ) ) > 0 ) ans += ( arr [ i ] - ( 1 * i ) ) ; if ( ( arr [ i ] - ( 1 * i ) ) == 0 ) break ; } return ans ; } int main ( ) { int arr [ ] = { 6 , 6 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxProfit ( arr , n ) ; return 0 ; }