Given two positive integer a and b and a range [low, high]. The task is to find the gretest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.
Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1, 5] and is common divisor of 9 and 27. Input : a = 9, b = 27, low = 10, high = 11 Output : -1
The idea is to find the Greatest Common Divisor GCD(a, b) of a and b. Now observe, divisor of GCD(a, b) is also the divisor of a and b. So, we will iterate a loop i from 1 to sqrt(GCD(a, b)) and check if i divides GCD(a, b). Also, observe if i is divisor of GCD(a, b) then GCD(a, b)/i will also be divisor. So, for each iteration, if i divides GCD(a, b), we will find maximimum of i and GCD(a, b)/i if they lie in the range.
Below is the implementation of this approach:
- Find if a point lies inside a Circle
- Find a point that lies inside exactly K given squares
- Find XOR of numbers from the range [L, R]
- Find the occurrences of digit d in the range [0..n]
- Find numbers with n-divisors in a given range
- Find numbers with K odd divisors in a given range
- Find the largest twins in given range
- Find the smallest twins in given range
- Count the number of intervals in which a given value lies
- Check whether the point (x, y) lies on a given line
- Find the number of divisors of all numbers in the range [1, n]
- Queries to update a given index and find gcd in range
- Find a range of composite numbers of given length
- Find a distinct pair (x, y) in given range such that x divides y
- How to check if a given point lies inside or outside a polygon?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.