Find the minimum difference between Shifted tables of two numbers
Last Updated :
16 Oct, 2023
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:
- Compute the Greatest Common Divisor (GCD) of the numbers ‘a’ and ‘b’. Let the GCD of two numbers be ‘g’.
- Compute the absolute difference “diff” of the shifts ‘x’ and ‘y’ under modulo ‘g’, i.e., diff = abs(a – b) % g
- The required shifted difference is minimum of ‘diff’ and ‘g – diff’.
Below is implementation of above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
int findMinDiff( int a, int b, int x, int y)
{
int g = gcd(a,b);
int diff = abs (x-y) % g;
return min(diff, g - diff);
}
int main()
{
int a = 20, b = 52, x = 5, y = 7;
cout << findMinDiff(a, b, x, y) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int gcd( int a, int b)
{
while (b != 0 )
{
int t = b;
b = a % b;
a = t;
}
return a;
}
static int findMinDiff( int a, int b,
int x, int y)
{
int g = gcd(a,b);
int diff = Math.abs(x - y) % g;
return Math.min(diff, g - diff);
}
public static void main (String[] args)
{
int a = 20 , b = 52 , x = 5 , y = 7 ;
System.out.println( findMinDiff(a, b, x, y));
}
}
|
Python3
import math as mt
def gcd(a,b):
while (b ! = 0 ):
t = b
b = a % b
a = t
return a
def findMinDiff (a, b, x, y):
g = gcd(a,b)
diff = abs (x - y) % g
return min (diff, g - diff)
a,b,x,y = 20 , 52 , 5 , 7
print (findMinDiff(a, b, x, y))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
static int findMinDiff( int a, int b,
int x, int y)
{
int g = gcd(a, b);
int diff = Math.Abs(x - y) % g;
return Math.Min(diff, g - diff);
}
static void Main()
{
int a = 20, b = 52, x = 5, y = 7;
Console.WriteLine(findMinDiff(a, b, x, y));
}
}
|
Javascript
<script>
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));
</script>
|
PHP
<?php
function gcd( $a , $b )
{
while ( $b != 0)
{
$t = $b ;
$b = $a % $b ;
$a = $t ;
}
return $a ;
}
function findMinDiff( $a , $b , $x , $y )
{
$g = gcd( $a , $b );
$diff = abs ( $x - $y ) % $g ;
return min( $diff , $g - $diff );
}
$a = 20; $b = 52; $x = 5; $y = 7;
echo findMinDiff( $a , $b , $x , $y ), "\n" ;
?>
|
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;
int gcd( int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
int findMinDiff( int a, int b, int x, int y)
{
int g = gcd(a, b);
int diff = abs (x - y) % g;
return min(diff, g - diff);
}
int main()
{
int a = 10, b = 15, x = 2, y = 5;
cout << findMinDiff(a, b, x, y) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.Scanner;
public class Main {
public static int gcd( int a, int b) {
while (b != 0 ) {
int t = b;
b = a % b;
a = t;
}
return a;
}
public static int findMinDiff( int a, int b, int x, int y) {
int g = gcd(a, b);
int diff = Math.abs(x - y) % g;
return Math.min(diff, g - diff);
}
public static void main(String[] args) {
int a = 10 , b = 15 , x = 2 , y = 5 ;
System.out.println(findMinDiff(a, b, x, y));
}
}
|
Python
def gcd(a, b):
while b ! = 0 :
t = b
b = a % b
a = t
return a
def find_min_diff(a, b, x, y):
g = gcd(a, b)
diff = abs (x - y) % g
return min (diff, g - diff)
def main():
a, b, x, y = 10 , 15 , 2 , 5
print (find_min_diff(a, b, x, y))
if __name__ = = "__main__" :
main()
|
C#
using System;
public class Program
{
public static int GCD( int a, int b)
{
while (b != 0)
{
int t = b;
b = a % b;
a = t;
}
return a;
}
public static int FindMinDiff( int a, int b, int x, int y)
{
int g = GCD(a, b);
int diff = Math.Abs(x - y) % g;
return Math.Min(diff, g - diff);
}
public static void Main( string [] args)
{
int a = 10, b = 15, x = 2, y = 5;
Console.WriteLine(FindMinDiff(a, b, x, y));
}
}
|
Javascript
<script>
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 = 10, b = 15, x = 2, y = 5;
console.log(findMinDiff(a, b, x, y));
<script>
|
- 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
Please Login to comment...