Given two positive numbers x and y. Find the maximum valued integer a such that:
- a divides x i.e. x % a = 0
- a and y are co-prime i.e. gcd(a, y) = 1
Input : x = 15 y = 3 Output : a = 5 Explanation: 5 is the max integer which satisfies both the conditions. 15 % 5 =0 gcd(5, 3) = 1 Hence, output is 5. Input : x = 14 y = 28 Output : a = 1 Explanation: 14 % 1 =0 gcd(1, 28) = 1 Hence, output is 1.
Approach: Here, first we will remove the common factors of x and y from x by finding the greatest common divisor (gcd) of x and y and dividing x with that gcd.
x = x / gcd(x, y) —— STEP1
Now, we repeat STEP1 till we get gcd(x, y) = 1.
At last, we return a = x
5 1 7
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.
- 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
- Largest number less than or equal to N/2 which is coprime to N
- Largest Coprime Set Between two integers
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Find the largest composite number that divides N but is strictly lesser than N
- Legendre's formula (Given p and n, find the largest x such that p^x divides n!)
- Composite XOR and Coprime AND
- Coprime divisors of a number
- Count all pairs of divisors of a number N whose sum is coprime with N
- Count of integers up to N which are non divisors and non coprime with N
- Finding a Non Transitive Coprime Triplet in a Range
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Print all Coprime path of a Binary Tree
- Print all distinct Coprime sets possible from 1 to N
- Check if all the pairs of an array are coprime with each other
- Check if a M-th fibonacci number divides N-th fibonacci number
- Highest power of a number that divides other number
- Count of prime digits of a Number which divides the number
- Minimum value that divides one number and divisible by other
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.
Improved By : jit_t