Given two integers **p** and **q**, the task is to find the smallest number **K** such that **K % p = 0** and **q % K = 0**. If no such **K** is possible then print **-1**.

**Examples:**

Input:p = 2, q = 8

Output:2

2 % 2 = 0 and 8 % 2 = 0

Input:p = 5, q = 14

Output:-1

**Approach:** In order for **K** to be possible, **q** must be divisible by **p**.

- If
**q % p = 0**then print**p** - Else print
**-1**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum ` `// value K such that K % p = 0 ` `// and q % k = 0 ` `int` `getMinVal(` `int` `p, ` `int` `q) ` `{ ` ` ` ` ` `// If K is possible ` ` ` `if` `(q % p == 0) ` ` ` `return` `p; ` ` ` ` ` `// No such K is possible ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `p = 24, q = 48; ` ` ` `cout << getMinVal(p, q); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum ` `// value K such that K % p = 0 ` `// and q % k = 0 ` `static` `int` `getMinVal(` `int` `p, ` `int` `q) ` `{ ` ` ` ` ` `// If K is possible ` ` ` `if` `(q % p == ` `0` `) ` ` ` `return` `p; ` ` ` ` ` `// No such K is possible ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `p = ` `24` `, q = ` `48` `; ` ` ` `System.out.println(getMinVal(p, q)); ` `} ` `} ` ` ` `// This code is contributed by jit_t. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum ` `# value K such that K % p = 0 ` `# and q % k = 0 ` `def` `getMinVal(p, q): ` ` ` ` ` `# If K is possible ` ` ` `if` `q ` `%` `p ` `=` `=` `0` `: ` ` ` `return` `p ` ` ` ` ` `# No such K is possible ` ` ` `return` `-` `1` ` ` `# Driver code ` `p ` `=` `24` `; q ` `=` `48` `print` `(getMinVal(p, q)) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum ` `// value K such that K % p = 0 ` `// and q % k = 0 ` `static` `int` `getMinVal(` `int` `p, ` `int` `q) ` `{ ` ` ` ` ` `// If K is possible ` ` ` `if` `(q % p == 0) ` ` ` `return` `p; ` ` ` ` ` `// No such K is possible ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `p = 24, q = 48; ` ` ` `Console.WriteLine(getMinVal(p, q)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Code_Mech. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the minimum ` `// value K such that K % p = 0 ` `// and q % k = 0 ` `function` `getMinVal(` `$p` `, ` `$q` `) ` `{ ` ` ` ` ` `// If K is possible ` ` ` `if` `(` `$q` `% ` `$p` `== 0) ` ` ` `return` `$p` `; ` ` ` ` ` `// No such K is possible ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `$p` `= 24; ` `$q` `= 48; ` `echo` `getMinVal(` `$p` `, ` `$q` `); ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

24

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.

## Recommended Posts:

- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find smallest number n such that n XOR n+1 equals to given k.
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Find K'th smallest number such that A + B = A | B
- Find smallest values of x and y such that ax - by = 0
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Find Kth smallest value for b such that a + b = a | b
- Smallest number S such that N is a factor of S factorial or S!
- Smallest number k such that the product of digits of k is equal to n
- Smallest number greater or equals to N such that it has no odd positioned bit set
- Find smallest number with given number of digits and sum of digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Partition N into M parts such that difference between Max and Min part is smallest
- Find the smallest number whose digits multiply to a given number n
- Find smallest possible Number from a given large Number with same count of digits
- Find smallest number formed by inverting digits of given number N
- Smallest index such that there are no 0 or 1 to its right
- Smallest x such that 1*n, 2*n, ... x*n have all digits from 1 to 9
- Smallest divisor D of N such that gcd(D, M) is greater than 1

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.