Skip to content
Related Articles

Related Articles

Improve Article

Find the minimum difference between Shifted tables of two numbers

  • Difficulty Level : Hard
  • Last Updated : 31 Mar, 2021

Given two numbers ‘a’ and ‘b’. Find the minimum difference between any terms in shifted infinite tables of ‘a’ and ‘b’, given shifts ‘x’ and ‘y’, where x, y >= 0.
Let us consider a = 6 and b = 16. Infinite tables of these numbers are.
Table of a : 6, 12, 18, 24, 30, 36, 42, 48.…. 
Table of b : 16, 32, 48, 64, 80, 96, 112, 128…..
Let given shifts be x = 5 and y = 2 
Shifted Table of a : 11, 17, 23, 29, 35, 41, 47 … 
Shifted Table of b : 18, 34, 50, 66, 82, 98, 114 …
The minimum difference between any two terms of above two shifted tables is 1 (See colored terms). So the output should be 1.
We strongly recommend you to minimize your browser and try this yourself first.
Algorithm: 
 

  1. Compute the Greatest Common Divisor (GCD) of the numbers ‘a’ and ‘b’. Let the GCD of two numbers be ‘g’.
  2. Compute the absolute difference “diff” of the shifts ‘x’ and ‘y’ under modulo ‘g’, i.e., diff = abs(a – b) % g
  3. The required shifted difference is minimum of ‘diff’ and ‘g – diff’.

Below is implementation of above algorithm.
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

C++




// C++ program to find the minimum difference
// between any two terms of two tables
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to find GCD of a and b
int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}
 
// 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'.
int findMinDiff(int a, int b, int x, int y)
{
    // Calculate gcd of a nd b
    int g  = gcd(a,b);
 
    // Calculate difference between x and y
    int diff = abs(x-y) % g;
 
    return min(diff, g - diff);
}
 
// Driver Code
int main()
{
    int a = 20, b = 52, x = 5, y = 7;
 
    cout << findMinDiff(a, b, x, y) << endl;
 
    return 0;
}

Java




// Java program to find the
// minimum difference between
// any two terms of two tables
import java.io.*;
 
class GFG
{
     
// Utility function to
// find GCD of a and b
static int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}
 
// 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'.
static int findMinDiff(int a, int b,
                       int x, int y)
{
    // Calculate gcd
    // of a nd b
    int g = gcd(a,b);
 
    // Calculate difference
    // between x and y
    int diff = Math.abs(x - y) % g;
 
    return Math.min(diff, g - diff);
}
 
// Driver Code
public static void main (String[] args)
{
    int a = 20, b = 52, x = 5, y = 7;
    System.out.println( findMinDiff(a, b, x, y));
}
}
 
// This code is contributed by ajit

Python3




# python3 program to find the minimum difference
# between any two terms of two tables
import math as mt
   
# Utility function to find GCD of a and b
def gcd(a,b):
 
    while (b != 0):
        t = b
        b = a % b
        a = t
 
    return a
  
# 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'.
def findMinDiff (a, b, x, y):
    # Calculate gcd of a nd b
    = gcd(a,b)
   
    # Calculate difference between x and y
    diff = abs(x-y) % g
   
    return min(diff, g - diff)
   
# Driver Code
a,b,x,y = 20,52,5,7
 
print(findMinDiff(a, b, x, y))
#This code is contributed by Mohit kumar 29

C#




// C# program to find the minimum difference
// between any two terms of two tables
using System;
class GFG
{
     
// Utility function to find GCD of a and b
static int gcd(int a, int b)
{
    while (b != 0)
    {
        int t = b;
        b = a % b;
        a = t;
    }
    return a;
}
 
// 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'.
static int findMinDiff(int a, int b,
                       int x, int y)
{
    // Calculate gcd
    // of a nd b
    int g = gcd(a, b);
 
    // Calculate difference
    // between x and y
    int diff = Math.Abs(x - y) % g;
 
    return Math.Min(diff, g - diff);
}
 
// Driver Code
static void Main()
{
    int a = 20, b = 52, x = 5, y = 7;
    Console.WriteLine(findMinDiff(a, b, x, y));
}
}
 
// This code is contributed by mits

PHP




<?php
// PHP program to find the minimum
// difference between any two terms
// of two tables
 
// Utility function to
// find GCD of a and b
function gcd($a, $b)
{
    while ($b != 0)
    {
        $t = $b;
        $b = $a % $b;
        $a = $t;
    }
    return $a;
}
 
// 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'.
function findMinDiff($a, $b, $x, $y)
{
    // Calculate gcd of a nd b
    $g = gcd($a, $b);
 
    // Calculate difference
    // between x and y
    $diff = abs($x - $y) % $g;
 
    return min($diff, $g - $diff);
}
 
// Driver Code
$a = 20; $b = 52; $x = 5; $y = 7;
 
echo findMinDiff($a, $b, $x, $y), "\n";
 
// This code is contributed by aj_36
?>

Javascript




<script>
 
// Javascript program to find the
// minimum difference between
// any two terms of two tables
 
// Utility function to
// find GCD of a and b
function gcd(a, b)
{
    while (b != 0)
    {
        let t = b;
        b = a % b;
        a = t;
    }
    return a;
}
   
// 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'.
function findMinDiff(a, b,
                       x, y)
{
    // Calculate gcd
    // of a and b
    let g = gcd(a,b);
   
    // Calculate difference
    // between x and y
    let diff = Math.abs(x - y) % g;
   
    return Math.min(diff, g - diff);
}
   
 
// Driver Code
 
    let a = 20, b = 52, x = 5, y = 7;
    document.write( findMinDiff(a, b, x, y));
         
                       
</script>

Output : 

 2

Complexity: O(log n), to compute the GCD.
How does this work?
After shifting tables become (Assuming y > x and diff = y-x) 
a+x, 2a+x, 3a+x, .. ab+x, …. and b+y, 2b+y, 3b+y, …., ab+y, ….
In general, difference is, abs[(am + x) – (bn + y)] where m >= 1 and n >= 1
An upper bound on minimum difference is “abs(x – y)”. We can always get this difference by putting m = b and n = a.
How can we reduce the absolute difference below “abs(x – y)”? 
Let us rewrite “abs[(am + x) – (bn + y)]” as abs[(x – y) – (bn – am)]
Let the GCD of ‘a’ and ‘b’ be ‘g’. Let us consider the table of ‘g’. The table has all terms like a, 2a, 3a, … b, 2b, 3b, … and also the terms (bn – am) and (am – bn). 
 

This article is contributed by Vaibhav Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :