Smallest possible integer to be added to N | Function to find the smallest positive integer X such that X is added to every element of A except one element to give array B ; Stores the unique elements of array A ; Insert A [ i ] into the set s ; Sort array A [ ] ; Sort array B [ ] ; Assume X value as B [ 0 ] - A [ 1 ] ; Check if X value assumed is negative or 0 ; Update the X value to B [ 0 ] - A [ 0 ] ; If assumed value is wrong ; Update X value ; Driver Code
def findVal ( A , B , N ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT s . add ( A [ i ] ) NEW_LINE DEDENT A . sort ( ) NEW_LINE B . sort ( ) NEW_LINE X = B [ 0 ] - A [ 1 ] NEW_LINE if ( X <= 0 ) : NEW_LINE INDENT X = B [ 0 ] - A [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( N - 1 ) : NEW_LINE INDENT if ( B [ i ] - X not in s ) : NEW_LINE INDENT X = B [ 0 ] - A [ 0 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( X ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 3 , 8 ] NEW_LINE B = [ 15 , 8 , 11 ] NEW_LINE N = len ( A ) NEW_LINE findVal ( A , B , N ) NEW_LINE DEDENT