# Pairs with GCD equal to one in the given range

Given a range i.e. L and R, the task is to find if we can form pairs such that GCD of every pair is 1. Every number in the range L-R should be involved exactly in one pair.

Examples:

```Input: L = 1, R = 8
Output: Yes
{2, 7}, {4, 1}, {3, 8}, {6, 5}
All pairs have GCD as 1.

Input: L = 2, R = 4
Output: No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Since every number in the range(L, R) must be included exactly once in every pair. Hence if L-R is an even number, then it is not possible. If L-R is an odd number, then print all the adjacent pairs since adjacent pairs will always have GCD as 1.

Below is the implementation of the above approach:

## C++

 `// C++ program to print all pairs ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print all pairs ` `bool` `checkPairs(``int` `l, ``int` `r) ` `{ ` `    ``// check if even ` `    ``if` `((l - r) % 2 == 0)  ` `        ``return` `false``; ` ` `  `    ``/* We can print all adjacent pairs ` `      ``for (int i = l; i < r; i += 2) { ` `          ``cout << "{" << i << ", " << i + 1 << "}, "; ` `      ``} */` `      `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `l = 1, r = 8; ` `    ``if` `(checkPairs(l, r)) ` `       ``cout << ``"Yes"``; ` `    ``else` `       ``cout << ``"No"``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to print all pairs ` `class` `GFG ` `{ ` `// Function to print all pairs ` `static` `boolean` `checkPairs(``int` `l, ``int` `r) ` `{ ` `    ``// check if even ` `    ``if` `((l - r) % ``2` `== ``0``)  ` `        ``return` `false``; ` ` `  `    ``/* We can print all adjacent pairs ` `    ``for (int i = l; i < r; i += 2)  ` `    ``{ ` `        ``System.out.print("{"+i+", "+i + 1+"}, "); ` `    ``} */` `     `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `l = ``1``, r = ``8``; ` `    ``if` `(checkPairs(l, r)) ` `    ``System.out.println(``"Yes"``); ` `    ``else` `    ``System.out.println(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by mits `

## Python 3

 `# Python 3 program to print all pairs  ` ` `  `# Function to print all pairs  ` `def` `checkPairs(l, r) : ` ` `  `    ``# check if even  ` `    ``if` `(l ``-` `r) ``%` `2` `=``=` `0` `: ` `        ``return` `False` ` `  `    ``""" we can print all adjacent pairs ` `    ``for i in range(l,r,2) : ` `        ``print("{",i,",",i + 1, "},") ` `    ``"""` `     `  `    ``return` `True` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``l, r ``=` `1``, ``8` ` `  `    ``if` `checkPairs(l, r) : ` `        ``print``(``"Yes"``) ` `    ``else` `: ` `        ``print``(``"No"``) ` `            `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# program to print all pairs ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// Function to print all pairs ` `static` `bool` `checkPairs(``int` `l, ``int` `r) ` `{ ` `    ``// check if even ` `    ``if` `((l - r) % 2 == 0)  ` `        ``return` `false``; ` ` `  `    ``/* We can print all adjacent pairs ` `    ``for (int i = l; i < r; i += 2)  ` `    ``{ ` `        ``System.out.print("{"+i+", "+i + 1+"}, "); ` `    ``} */` `     `  `    ``return` `true``; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `l = 1, r = 8; ` `    ``if` `(checkPairs(l, r)) ` `    ``Console.Write(``"Yes"``); ` `    ``else` `    ``Console.Write(``"No"``); ` `} ` `} ` ` `  `// This code is contributed by Raj `

## PHP

 ` `

Output:

```Yes
```

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

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.