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
def maxCntRemovedfromArray ( arr , N , brr , M ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE i = 0 NEW_LINE sumArr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sumArr += arr [ i ] NEW_LINE DEDENT sumBrr = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT sumBrr += brr [ i ] NEW_LINE DEDENT cntRemElem = 0 NEW_LINE while ( i < N and sumArr >= sumBrr ) : NEW_LINE INDENT sumArr -= arr [ i ] NEW_LINE i += 1 NEW_LINE if ( sumArr >= sumBrr ) : NEW_LINE INDENT cntRemElem += 1 NEW_LINE DEDENT DEDENT return cntRemElem NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 4 , 6 ] NEW_LINE brr = [ 7 ] NEW_LINE N = len ( arr ) NEW_LINE M = len ( brr ) NEW_LINE print ( maxCntRemovedfromArray ( arr , N , brr , M ) ) NEW_LINE DEDENT