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.
Input: p = 3, q = 99
99 % 3 = 0
3 % 3 = 0
Input: p = 2, q = 7
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:
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.
- Minimum number of swaps required to make a number divisible by 60
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum and Maximum element of an array which is divisible by a given number k
- Minimum number of given moves required to make N divisible by 25
- Form N by adding 1 or 2 in minimum number of operations X where X is divisible by M
- Partitions possible such that the minimum element divides all the other elements of the partition
- Find two co-prime integers such that the first divides A and the second divides B
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Count of prime digits of a Number which divides the number
- Highest power of a number that divides other number
- Highest power of two that divides a given number
- Largest number that divides x and is co-prime with y
- Find the Smallest number that divides X^X
- Check if the sum of digits of a number N divides it
- Check if a given number divides the sum of the factorials of its digits
- Greatest divisor which divides all natural number in range [L, R]
- Highest power of 2 that divides a number represented in binary
- Find maximum power of a number that divides a factorial
- Find a number that divides maximum array elements
- Find the largest composite number that divides N but is strictly lesser than N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.