Maximize count of persons receiving a chocolate | Python3 program for the above approach ; Function to count the maximum number of persons receiving a chocolate ; Initialize count as 0 ; Sort the given arrays ; Initialize a multiset ; Insert B [ ] array values into the multiset ; Traverse elements in array A [ ] ; Search for the lowest value in B [ ] ; If found , increment count , and delete from set ; Return the number of people ; Driver Code ; Function Call
from bisect import bisect_left NEW_LINE def countMaxPersons ( A , n , B , m , x ) : NEW_LINE INDENT count = 0 NEW_LINE A = sorted ( A ) NEW_LINE B = sorted ( B ) NEW_LINE s = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT s . append ( B [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT val = A [ i ] - x NEW_LINE it = bisect_left ( s , val ) NEW_LINE if ( it != len ( s ) and it <= A [ i ] + x ) : NEW_LINE INDENT count += 1 NEW_LINE del s [ it ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 90 , 49 , 20 , 39 , 49 ] NEW_LINE B = [ 14 , 24 , 82 ] NEW_LINE X = 15 NEW_LINE N = len ( A ) NEW_LINE M = len ( B ) NEW_LINE print ( countMaxPersons ( A , N , B , M , X ) ) NEW_LINE DEDENT