Smallest Difference pair of values between two unsorted Arrays | Python 3 Code to find Smallest Difference between two Arrays ; function to calculate Small result between two arrays ; Sort both arrays using sort function ; Initialize result as max value ; Scan Both Arrays upto sizeof of the Arrays ; Move Smaller Value ; return final sma result ; Input given array A ; Input given array B ; Calculate size of Both arrays ; Call function to print smallest result
import sys NEW_LINE def findSmallestDifference ( A , B , m , n ) : NEW_LINE INDENT A . sort ( ) NEW_LINE B . sort ( ) NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE result = sys . maxsize NEW_LINE while ( a < m and b < n ) : NEW_LINE INDENT if ( abs ( A [ a ] - B [ b ] ) < result ) : NEW_LINE INDENT result = abs ( A [ a ] - B [ b ] ) NEW_LINE DEDENT if ( A [ a ] < B [ b ] ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT else : NEW_LINE INDENT b += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT A = [ 1 , 2 , 11 , 5 ] NEW_LINE B = [ 4 , 12 , 19 , 23 , 127 , 235 ] NEW_LINE m = len ( A ) NEW_LINE n = len ( B ) NEW_LINE print ( findSmallestDifference ( A , B , m , n ) ) NEW_LINE