Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | Java program to implement the above approach ; Function to maximize the count of elements required to be removed from arr [ ] such that the sum of arr [ ] is greater than or equal to sum of the array brr [ ] ; Sort the array arr [ ] ; Stores index of smallest element of arr [ ] ; Stores sum of elements of the array arr [ ] ; Traverse the array arr [ ] ; Update sumArr ; Stores sum of elements of the array brr [ ] ; Traverse the array brr [ ] ; Update sumArr ; Stores count of removed elements ; Repeatedly remove the smallest element of arr [ ] ; Update sumArr ; Remove the smallest element ; If the sum of remaining elements in arr [ ] >= sum of brr [ ] ; Update cntRemElem ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static int maxCntRemovedfromArray ( int [ ] arr , int N , int [ ] brr , int M ) { Arrays . sort ( arr ) ; int i = 0 ; int sumArr = 0 ; for ( i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } int sumBrr = 0 ; for ( i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } int cntRemElem = 0 ; while ( i < N && sumArr >= sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 4 , 6 } ; int [ ] brr = new int [ ] { 7 } ; int N = arr . length ; int M = brr . length ; System . out . println ( maxCntRemovedfromArray ( arr , N , brr , M ) ) ; } }