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
function findVal ( A , B , N ) { let s = new Set ( ) ; for ( let i = 0 ; i < N ; i ++ ) { s . add ( A [ i ] ) ; } A . sort ( function ( a , b ) { return a - b } ) ; B . sort ( function ( a , b ) { return a - b } ) let X = B [ 0 ] - A [ 1 ] ; if ( X <= 0 ) X = B [ 0 ] - A [ 0 ] ; else { for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( s . has ( B [ i ] - X ) == false ) { X = B [ 0 ] - A [ 0 ] ; break ; } } } document . write ( X ) ; } let A = [ 1 , 4 , 3 , 8 ] ; let B = [ 15 , 8 , 11 ] ; let N = A . length ; findVal ( A , B , N ) ;