Open In App

Find the minimum difference between Shifted tables of two numbers

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.
 

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


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>


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
?>


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). 

Approach 2:

In this approach, we first calculate the GCD of a and b using the gcd function. For the given inputs of a = 10 and b = 15, the GCD is 5.

We then calculate the absolute difference between x and y, which is 3.

Finally, we calculate the minimum difference between any two terms of the shifted tables by taking the modulo of the absolute difference and the GCD, and returning the minimum of this value and g – diff, where g is the GCD and diff is the absolute difference between x and y.

For the given inputs of x = 2 and y = 5, the minimum difference between any two terms of the shifted tables is 2.

C++




#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 = 10, b = 15, x = 2, y = 5;
 
    cout << findMinDiff(a, b, x, y) << endl;
 
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
import java.util.Scanner;
 
public class Main {
 
    // Utility function to find GCD of a and b
    public 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'.
    public static int findMinDiff(int a, int b, int x, int y) {
        // Calculate gcd of a and 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 = 10, b = 15, x = 2, y = 5;
 
        System.out.println(findMinDiff(a, b, x, y));
    }
}
//This code is contributed by aeroabrar_31


Python




# code
# 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 find_min_diff(a, b, x, y):
    # Calculate gcd of a and b
    g = gcd(a, b)
 
    # Calculate difference between x and y
    diff = abs(x - y) % g
 
    return min(diff, g - diff)
 
# Driver Code
def main():
    a, b, x, y = 10, 15, 2, 5
 
    print(find_min_diff(a, b, x, y))
 
if __name__ == "__main__":
    main()
 
#This code is contributed by aeroabrar_31


C#




using System;
 
public class Program
{
    // Utility function to find GCD of a and b
    public 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'.
    public static int FindMinDiff(int a, int b, int x, int y)
    {
        // Calculate gcd of a and 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 = 10, b = 15, x = 2, y = 5;
 
        Console.WriteLine(FindMinDiff(a, b, x, y));
    }
}


Javascript




<script>
 
// 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 = 10, b = 15, x = 2, y = 5;
 
console.log(findMinDiff(a, b, x, y));
 
 
<script>


Output

2









  1. gcd function:
  • Time complexity: O(log(min(a, b)))
  • Space complexity: O(1)
     

       2. findMinDiff function:

  •  Time complexity: O(log(min(a, b)))
  • Space complexity: O(1)


 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads