Given three positive integer L, R, G. The task is to find the count of the pair (x,y) having GCD(x,y) = G and x, y lie between L and R.
Input : L = 1, R = 11, G = 5 Output : 3 (5, 5), (5, 10), (10, 5) are three pair having GCD equal to 5 and lie between 1 and 11. So answer is 3. Input : L = 1, R = 10, G = 7 Output : 1
A simple solution is to go through all pairs in [L, R]. For every pair, find its GCD. If GCD is equal to g, then increment count. Finally return count.
An efficient solution is based on the fact that, for any positive integer pair (x, y) to have GCD equal to g, x and y should be divisible by g.
Observe, there will be at most (R – L)/g numbers between L and R which are divisible by g.
So we find numbers between L and R which are divisible by g. For this, we start from ceil(L/g) * g and with increment by g at each step while it doesn’t exceed R, count numbers having GCD equal to 1.
ceil(L/g) * g = floor((L + g - 1) / g) * g.
Below is the implementation of above idea :
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Maximum GCD among all pairs (i, j) of first N natural numbers
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count of all possible pairs having sum of LCM and GCD equal to N
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Count number of subsets of a set with GCD equal to a given number
- Pairs with GCD equal to one in the given range
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Count of pairs with sum N from first N natural numbers
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Rearrange array to make it non-decreasing by swapping pairs having GCD equal to minimum array element
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Given GCD G and LCM L, find number of possible pairs (a, b)
- Maximum LCM among all pairs (i, j) of first N natural numbers
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Count of quadruplets from range [L, R] having GCD equal to K
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
Improved By : vt_m