Minimum Difference between multiples of three integers
Last Updated :
20 Aug, 2022
Given three integers X, Y, and Z. the task is to find the minimum distance between any two multiples of the given integers.
Note: Common multiples are not considered.
Examples:
Input: X = 5, Y = 2, Z = 3
Output: 1
Explanation: The multiples if arranged in sorted order are 0, 2, 3, 4, 5, 6, 8. . .
Out of which the minimum possible difference is 1 between 2 and 3.
Input: X = 3, Y = 6, Z = 12
Output: 3
Approach: To solve the problem follow the below idea:
- Difference between the multiples of two numbers A and B is actually the multiples of GCD(A, B).
- To calculate the minimum possible difference between the multiple of any two numbers, calculate the GCD of those two numbers.
Follow the given steps to solve the problem:
- Calculate the greatest common divisor(GCD) between every pair of numbers.
- Return the minimum value by comparing the values calculated in the above step.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumdifference( int H1, int H2, int H3)
{
int ans = 0;
int d1, d2, d3;
d3 = __gcd(H1, H3);
d1 = __gcd(H1, H2);
d2 = __gcd(H2, H3);
ans = min(d1, d2);
ans = min(d3, ans);
return ans;
}
int main()
{
int X = 5, Y = 2, Z = 3;
cout << minimumdifference(X, Y, Z);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int gcd( int a, int b)
{
return b== 0 ? a : gcd(b, a%b);
}
static int minimumdifference( int H1, int H2, int H3)
{
int ans= 0 ;
int d1, d2, d3;
d3 = gcd(H1, H3);
d1 = gcd(H1, H2);
d2 = gcd(H2, H3);
ans = Math.min(d1, d2);
ans = Math.min(d3, ans);
return ans;
}
public static void main (String[] args)
{
int X = 5 , Y = 2 , Z = 3 ;
System.out.println(minimumdifference(X, Y, Z));
}
}
|
Python3
def __gcd(x, y):
while (y):
x, y = y, x % y
return abs (x)
def minimumdifference(H1, H2, H3) :
ans = 0
d1,d2,d3 = 0 , 0 , 0
d3 = __gcd(H1, H3)
d1 = __gcd(H1, H2)
d2 = __gcd(H2, H3)
ans = min (d1, d2)
ans = min (d3, ans)
return ans
if __name__ = = "__main__" :
X,Y,Z = 5 , 2 , 3
print (minimumdifference(X, Y, Z))
|
C#
using System;
class GFG {
public static int gcd( int a, int b)
{
return b==0 ? a : gcd(b, a%b);
}
static int minimumdifference( int H1, int H2, int H3)
{
int ans=0;
int d1, d2, d3;
d3 = gcd(H1, H3);
d1 = gcd(H1, H2);
d2 = gcd(H2, H3);
ans = Math.Min(d1, d2);
ans = Math.Min(d3, ans);
return ans;
}
public static void Main()
{
int X = 5, Y = 2, Z = 3;
Console.Write(minimumdifference(X, Y, Z));
}
}
|
Javascript
<script>
const __gcd = (a, b) => {
if (!b) return a;
return __gcd(b, a % b);
}
const minimumdifference = (H1, H2, H3) => {
let ans = 0;
let d1, d2, d3;
d3 = __gcd(H1, H3);
d1 = __gcd(H1, H2);
d2 = __gcd(H2, H3);
ans = Math.min(d1, d2);
ans = Math.min(d3, ans);
return ans;
}
let X = 5, Y = 2, Z = 3;
document.write(minimumdifference(X, Y, Z));
</script>
|
Time Complexity: O(max(logX, logY, logZ))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...