Given a range [L, R], the task is to find all possible co-prime pairs from the range such that an element doesn’t appear in more than a single pair.
Input : L=1 ; R=6 Output : 3 The answer is 3 [(1, 2) (3, 4) (5, 6)], all these pairs have GCD 1. Input : L=2 ; R=4 Output : 1 The answer is 1 [(2, 3) or (3, 4)] as '3' can only be chosen for a single pair.
Approach : The key observation of the problem is that the numbers with the difference of ‘1’ are always relatively prime to each other i.e. co-primes.
GCD of this pair is always ‘1’. So, the answer will be (R-L+1)/2 [ (total count of numbers in range) / 2 ]
- If R-L+1 is odd then there will be one element left which can not form a pair.
- If R-L+1 is even then all elements can form pairs.
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.
- Print all distinct Coprime sets possible from 1 to N
- Count all pairs of divisors of a number N whose sum is coprime with N
- Check if all the pairs of an array are coprime with each other
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Finding a Non Transitive Coprime Triplet in a Range
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Print all Coprime path of a Binary Tree
- Queries to check if any non-repeating element exists within range [L, R] of an Array
- Sum of all odd length palindromic numbers within the range [L, R]
- Largest number less than or equal to N/2 which is coprime to 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.