Find the minimum difference between Shifted tables of two numbers | Utility function to find GCD of a and b ; Returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' . ; Calculate gcd of a and b ; Calculate difference between x and y ; Driver Code
function gcd ( a , b ) { while ( b != 0 ) { let t = b ; b = a % b ; a = t ; } return a ; } function findMinDiff ( a , b , x , y ) { let g = gcd ( a , b ) ; let diff = Math . abs ( x - y ) % g ; return Math . min ( diff , g - diff ) ; } let a = 20 , b = 52 , x = 5 , y = 7 ; document . write ( findMinDiff ( a , b , x , y ) ) ;