Find the Maximum possible Sum for the given conditions | Java 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
import java . util . Arrays ; import java . util . Collections ; public class GFG { static int maxProfit ( Integer [ ] arr ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int 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 ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 6 , 6 , 6 } ; System . out . println ( maxProfit ( arr ) ) ; } }