# Minimum value that divides one number and divisible by other

Given two integer p and q, the task is to find the minimum possible number x such that q % x = 0 and x % p = 0. If the conditions aren’t true for any number then print -1.

Examples:

Input: p = 3, q = 99
Output: 3
99 % 3 = 0
3 % 3 = 0

Input: p = 2, q = 7
Output: -1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: If a number x satisfies the given condition then it’s obvious that q will be divided by p i.e. q % p = 0 because x is a multiple of p and q is a multiple of x.
So the minimum possible value of x will be the GCD of p and q and when q is not divisible by p then no number will satisfy the given condition.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum valid number ` `// that satisfies the given conditions ` `int` `minValidNumber(``int` `p, ``int` `q) ` `{ ` `    ``// If possible ` `    ``if` `(q % p == 0) ` `        ``return` `__gcd(p, q); ` `    ``else` `        ``return` `-1; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `p = 2, q = 6; ` `    ``cout << minValidNumber(p, q); ` `    ``return` `0; ` `} `

## Java

 `//Java  implementation of the approach ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `    ``// Function to calculate gcd ` `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `    ``{ ` `         `  `        ``// Everything divides 0  ` `        ``if` `(a == ``0` `|| b == ``0``) ` `            ``return` `0``; ` `     `  `        ``// base case ` `        ``if` `(a == b) ` `            ``return` `a; ` `     `  `        ``// a is greater ` `        ``if` `(a > b) ` `            ``return` `__gcd(a - b, b); ` `             `  `        ``return` `__gcd(a, b - a); ` `    ``} ` ` `  `// Function to return the minimum valid number ` `// that satisfies the given conditions ` `static` `int` `minValidNumber(``int` `p, ``int` `q) ` `{ ` `    ``// If possible ` `    ``if` `(q % p == ``0``) ` `        ``return` `__gcd(p, q); ` `    ``else` `        ``return` `-``1``; ` `} ` ` `  `// Driver Code ` `    ``public` `static` `void` `main (String[] args) { ` `        ``int` `p = ``2``, q = ``6``; ` `        ``System.out.print(minValidNumber(p, q)); ` ` `  ` `  `    ``// THis code is contributed by  Sachin. ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the approach  ` `from` `math ``import` `gcd ` ` `  `# Function to return the minimum  ` `# valid number that satisfies the ` `# given conditions  ` `def` `minValidNumber(p, q) : ` ` `  `    ``# If possible  ` `    ``if` `(q ``%` `p ``=``=` `0``) :  ` `        ``return` `gcd(p, q)  ` `    ``else` `: ` `        ``return` `-``1` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``p, q ``=` `2``, ``6``;  ` `    ``print``(minValidNumber(p, q)) ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// Function to calculate gcd ` `    ``static` `int` `__gcd(``int` `a, ``int` `b) ` `    ``{ ` `          `  `        ``// Everything divides 0  ` `        ``if` `(a == 0 || b == 0) ` `            ``return` `0; ` `      `  `        ``// base case ` `        ``if` `(a == b) ` `            ``return` `a; ` `      `  `        ``// a is greater ` `        ``if` `(a > b) ` `            ``return` `__gcd(a - b, b); ` `              `  `        ``return` `__gcd(a, b - a); ` `    ``} ` ` `  `// Function to return the minimum valid number ` `// that satisfies the given conditions ` `static` `int` `minValidNumber(``int` `p, ``int` `q) ` `{ ` `    ``// If possible ` `    ``if` `(q % p == 0) ` `        ``return` `__gcd(p, q); ` `    ``else` `        ``return` `-1; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `p = 2, q = 6; ` `    ``Console.Write(minValidNumber(p, q)); ` `} ` `} ` ` `  `// THis code is contributed ` `// by Mukul Singh `

## PHP

 ` `

Output:

```2
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.