Maximizing Unique Pairs from two arrays | Returns count of maximum pairs that can be formed from a [ ] and b [ ] under given constraints . ; Sorting the first array . ; Sorting the second array . ; To keep track of visited elements of b [ ] ; For every element of a [ ] , find a pair for it and break as soon as a pair is found . ; Increasing the count if a pair is formed . ; Making the corresponding flag array element as 1 indicating the element in the second array element has been used . ; We break the loop to make sure an element of a [ ] is used only once . ; Driver code
def findMaxPairs ( a , b , n , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE flag = [ False ] * n NEW_LINE result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( abs ( a [ i ] - b [ j ] ) <= k and flag [ j ] == False ) : NEW_LINE INDENT result += 1 NEW_LINE flag [ j ] = True NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 10 , 15 , 20 ] NEW_LINE b = [ 17 , 12 , 24 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE print ( findMaxPairs ( a , b , n , k ) ) NEW_LINE DEDENT