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:
- Finding a Non Transitive Coprime Triplet in a Range
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Find a distinct pair (x, y) in given range such that x divides y
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Integers from the range that are composed of a single distinct digit
- Count of distinct remainders when N is divided by all the numbers from the range [1, N]
- Pairs with GCD equal to one in the given range
- Number of Co-prime pairs obtained from the sum of digits of elements in the given range
- Composite XOR and Coprime AND
- Count pairs in an array such that at least one element is prime
- Find unique pairs such that each element is less than or equal to N
- Number which has the maximum number of distinct prime factors in the range M to N
- Largest number less than or equal to N/2 which is coprime to N
- Partition first N natural number into two sets such that their sum is not coprime
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.