Tuesday, July 19, 2022
HomeWordPress DevelopmentMinimal Distinction between multiples of three integers

Minimal Distinction between multiples of three integers


View Dialogue

Enhance Article

Save Article

Like Article

Given three integers X, Y, and Z. the duty is to search out the minimal distance between any two multiples of the given integers.

Observe: Widespread multiples should not thought of. 

Examples:

Enter: X = 5, Y = 2, Z = 3
Output: 1
Clarification: The multiples if organized in sorted order are 0, 2, 3, 4, 5, 6, 8. . . 
Out of which the minimal doable distinction is 1 between 2 and three.

Enter: X = 3, Y = 6, Z = 12
Output: 3

 

Strategy: To unravel the issue observe the under concept:

  • Distinction between the multiples of two numbers A and B is definitely the multiples of GCD(A, B).
  • To calculate the minimal doable distinction between the a number of of any two numbers, calculate the GCD of these two numbers.

Comply with the given steps to unravel the issue:

  • Calculate the best frequent divisor(GCD) between each pair of numbers.
  • Return the minimal worth by evaluating the values calculated within the above step.

Under is the implementation for the above strategy:

C++

  

#embody <bits/stdc++.h>

utilizing 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 predominant()

{

    int X = 5, Y = 2, Z = 3;

  

    

    cout << minimumdifference(X, Y, Z);

    return 0;

}

Time Complexity: O(max(logX, logY, logZ))
Auxiliary Area: O(1)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments