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
function maxProfit ( arr ) { arr . sort ( ) ; arr . reverse ( ) ; let ans = 0 ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] - ( 1 * i ) ) > 0 ) ans += ( arr [ i ] - ( 1 * i ) ) ; if ( ( arr [ i ] - ( 1 * i ) ) == 0 ) break ; } return ans ; } let arr = [ 6 , 6 , 6 ] ; document . write ( maxProfit ( arr ) ) ;