Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | 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
function maxCntRemovedfromArray ( arr , N , brr , M ) { arr . sort ( ) ; let i = 0 ; let sumArr = 0 ; for ( i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } let sumBrr = 0 ; for ( i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } let cntRemElem = 0 ; while ( i < N && sumArr >= sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } let arr = [ 1 , 2 , 4 , 6 ] ; let brr = [ 7 ] ; let N = arr . length ; let M = brr . length ; document . write ( maxCntRemovedfromArray ( arr , N , brr , M ) ) ;