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.

**Examples:**

Input :L=1 ; R=6Output: 3 The answer is 3 [(1, 2) (3, 4) (5, 6)], all these pairs have GCD 1.Input :L=2 ; R=4Output :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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count possible pairs ` `void` `CountPair(` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `// total count of numbers in range ` ` ` `int` `x = (R - L + 1); ` ` ` ` ` `// Note that if 'x' is odd then ` ` ` `// there will be '1' element left ` ` ` `// which can't form a pair ` ` ` ` ` `// printing count of pairs ` ` ` `cout << x / 2 << ` `"\n"` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `L, R; ` ` ` ` ` `L = 1, R = 8; ` ` ` `CountPair(L, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

` ` `// Java implementation of the approach ` `import` `java.util.*; ` `class` `solution ` `{ ` ` ` `// Function to count possible pairs ` `static` `void` `CountPair(` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `// total count of numbers in range ` ` ` `int` `x = (R - L + ` `1` `); ` ` ` ` ` `// Note that if 'x' is odd then ` ` ` `// there will be '1' element left ` ` ` `// which can't form a pair ` ` ` ` ` `// printing count of pairs ` ` ` `System.out.println(x / ` `2` `+ ` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` ` ` `int` `L, R; ` ` ` ` ` `L = ` `1` `; R = ` `8` `; ` ` ` `CountPair(L, R); ` ` ` `} ` `} ` `//contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# the approach ` ` ` `# Function to count possible ` `# pairs ` `def` `CountPair(L,R): ` ` ` ` ` `# total count of numbers ` ` ` `# in range ` ` ` `x` `=` `(R` `-` `L` `+` `1` `) ` ` ` ` ` `# Note that if 'x' is odd then ` ` ` `# there will be '1' element left ` ` ` `# which can't form a pair ` ` ` `# printing count of pairs ` ` ` `print` `(x` `/` `/` `2` `) ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `L,R` `=` `1` `,` `8` ` ` `CountPair(L,R) ` ` ` `# This code is contributed by ` `# Indrajit Sinha. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to count possible pairs ` `static` `void` `CountPair(` `int` `L, ` `int` `R) ` `{ ` ` ` ` ` `// total count of numbers in range ` ` ` `int` `x = (R - L + 1); ` ` ` ` ` `// Note that if 'x' is odd then ` ` ` `// there will be '1' element left ` ` ` `// which can't form a pair ` ` ` ` ` `// printing count of pairs ` ` ` `Console.WriteLine(x / 2 + ` `"\n"` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `L, R; ` ` ` ` ` `L = 1; R = 8; ` ` ` `CountPair(L, R); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Function to count possible pairs ` `function` `CountPair(` `$L` `, ` `$R` `) ` `{ ` ` ` ` ` `// total count of numbers in range ` ` ` `$x` `= (` `$R` `- ` `$L` `+ 1); ` ` ` ` ` `// Note that if 'x' is odd then ` ` ` `// there will be '1' element left ` ` ` `// which can't form a pair ` ` ` ` ` `// printing count of pairs ` ` ` `echo` `$x` `/ 2, ` `"\n"` `; ` `} ` ` ` `// Driver code ` `$L` `= 1; ` `$R` `= 8; ` `CountPair(` `$L` `, ` `$R` `); ` ` ` `// This code is contributed by ANKITRAI1 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

4

**Complexity: O(1)**

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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.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.