Given two numbers n and m. Find the biggest integer a(gcd), such that all integers n, n + 1, n + 2, …, m are divisible by a.
Input : n = 1, m = 2 Output: 1 Explanation: Here, series become 1, 2. So, the greatest no which divides both of them is 1. Input : n = 475, m = 475 Output : 475 Explanation: Here, series has only one term 475. So, greatest no which divides 475 is 475.
Here, We have to examine only two cases:
- if a = b : the segment consists of a single number, hence the answer is a.
- if a < b : we have gcd(n, n + 1, n?+ 2, …, m) = gcd(gcd(n, n + 1), n + 2, …, m) = gcd(1, n + 2, …, n) = 1.
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Queries for GCD of all numbers of an array except elements in a given range
- Find the GCD that lies in given range
- Pairs with GCD equal to one in the given range
- Check if there is any pair in a given range with GCD is divisible by k
- Queries to update a given index and find gcd in range
- Find GCD of factorial of elements of given array
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of quadruplets from range [L, R] having GCD equal to K
- Find pair with maximum GCD for integers in range 2 to N
- Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
- Minimum gcd operations to make all array elements one
- GCD of elements which occur prime number of times
- GCD of elements occurring Fibonacci number of times in an Array
- Minimum elements to be added in a range so that count of elements is divisible by K
- Count number of subsets of a set with GCD equal to a given number
- Smallest Subarray with given GCD
- Count pairs of natural numbers with GCD equal to given number
- Given GCD G and LCM L, find number of possible pairs (a, b)
- GCD of digits of a given number
Below is the code for 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.
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.
Improved By : vt_m