# Co-prime pair with given sum minimum difference

Co-prime or mutually prime pair are those pair of numbers whose GCD is 1. Given a number n represent the number as the sum of a Co-prime pair ( A, B) such that A – B is minimum .

**Examples :**

Input : 12 Output : 5 7 Possible co-prime pairs are (5, 7), (1, 11) but difference between 5 and 7 is minimum Input : 13 Output : 6 7 Possible co-prime pairs are (6, 7), (5, 8), (4, 9), (3, 10), (2, 11) and (1, 12) but difference between 6 and 7 is minimum

A **simple solution** is to iterate through all number from 1 to n-1. For every number x, check if n – x and x are co-primes. If yes, then update the result if difference between these two is less than minimum difference so far.

An **efficient solution **is based on the fact that the numbers with minimum difference should be close to n/2. We loop from n/2 to 1. Check every possible pair and when first possible Co-prime pair is found display it and stop the loop.

## C++

`// CPP program to represent a number ` `// as sum of a co-prime pair such that ` `// difference between them is minimum ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to check if pair ` `// is co-prime or not ` `bool` `coprime(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `(__gcd(a, b) == 1); ` `} ` ` ` `// function to find and print ` `// co-prime pair ` `void` `pairSum(` `int` `n){ ` ` ` ` ` `int` `mid = n / 2; ` ` ` ` ` `for` `(` `int` `i = mid; i >= 1; i--) { ` ` ` `if` `(coprime(i, n - i) == 1) { ` ` ` `cout << i << ` `" "` `<< n - i; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `n = 11; ` ` ` `pairSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to represent a ` `// number as sum of a co-prime ` `// pair such that difference ` `// between them is minimum ` `class` `GFG ` `{ ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `b == ` `0` `? a : ` ` ` `__gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// function to check if pair ` ` ` `// is co-prime or not ` ` ` `static` `boolean` `coprime(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(__gcd(a, b) == ` `1` `); ` ` ` `} ` ` ` ` ` `// function to find and ` ` ` `// print co-prime pair ` ` ` `static` `void` `pairSum(` `int` `n) ` ` ` `{ ` ` ` `int` `mid = n / ` `2` `; ` ` ` ` ` `for` `(` `int` `i = mid; i >= ` `1` `; i--) ` ` ` `{ ` ` ` `if` `(coprime(i, n - i) == ` `true` `) ` ` ` `{ ` ` ` `System.out.print( i + ` `" "` `+ ` ` ` `(n - i)); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `11` `; ` ` ` `pairSum(n); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to represent ` `# a number as sum of a co-prime ` `# pair such that difference ` `# between them is minimum ` `import` `math ` ` ` `# function to check if pair ` `# is co-prime or not ` `def` `coprime(a, b): ` ` ` `return` `1` `if` `(math.gcd(a, b) ` `=` `=` `1` `) ` `else` `0` `; ` ` ` `# function to ` `# find and print ` `# co-prime pair ` `def` `pairSum(n): ` ` ` `mid ` `=` `int` `(n ` `/` `2` `); ` ` ` `i ` `=` `mid; ` ` ` `while` `(i >` `=` `1` `): ` ` ` `if` `(coprime(i, n ` `-` `i) ` `=` `=` `1` `): ` ` ` `print` `(i, n ` `-` `i); ` ` ` `break` `; ` ` ` `i ` `=` `i ` `-` `1` `; ` ` ` `# Driver code ` `n ` `=` `11` `; ` `pairSum(n); ` ` ` `# This code is contributed ` `# by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to represent a number ` `// as sum of a co-prime pair such that ` `// difference between them is minimum ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `b == 0 ? a : __gcd(b, a % b); ` ` ` `} ` ` ` ` ` `// function to check if pair ` ` ` `// is co-prime or not ` ` ` `static` `bool` `coprime(` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `return` `(__gcd(a, b) == 1); ` ` ` `} ` ` ` ` ` `// function to find and print ` ` ` `// co-prime pair ` ` ` `static` `void` `pairSum(` `int` `n) ` ` ` `{ ` ` ` `int` `mid = n / 2; ` ` ` ` ` `for` `(` `int` `i = mid; i >= 1; i--) ` ` ` `{ ` ` ` `if` `(coprime(i, n - i) == ` `true` `) ` ` ` `{ ` ` ` `Console.Write( i + ` `" "` ` ` `+ (n - i)); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 11; ` ` ` `pairSum(n); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to represent ` `// a number as sum of a ` `// co-prime pair such that ` `// difference between them ` `// is minimum ` ` ` `function` `gcd(` `$num1` `, ` `$num2` `) ` `{ ` ` ` `/* finds the greatest common ` `factor between two numbers */` `while` `(` `$num2` `!= 0) ` `{ ` ` ` `$t` `= ` `$num1` `% ` `$num2` `; ` ` ` `$num1` `= ` `$num2` `; ` ` ` `$num2` `= ` `$t` `; ` `} ` `return` `$num1` `; ` `} ` ` ` `// function to check if pair ` `// is co-prime or not ` `function` `coprime(` `$a` `, ` `$b` `) ` `{ ` ` ` `if` `(gcd(` `$a` `, ` `$b` `) == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `return` `0; ` `} ` ` ` `// function to find and ` `// print co-prime pair ` `function` `pairSum(` `$n` `) ` `{ ` ` ` `$mid` `= (int)((` `$n` `/ 2)); ` ` ` ` ` `for` `(` `$i` `= ` `$mid` `; ` `$i` `>= 1; ` `$i` `--) ` ` ` `{ ` ` ` `if` `(coprime(` `$i` `, ` `$n` `- ` `$i` `) == 1) ` ` ` `{ ` ` ` `echo` `$i` `. ` `" "` `. (` `$n` `- ` `$i` `); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `$n` `= 11; ` `pairSum(` `$n` `); ` ` ` `// This code is contributed ` `// by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5 6

This article is contributed by **Dibyendu Roy Chaudhuri**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Pair of prime numbers with a given sum and minimum absolute difference
- Count ways of choosing a pair with maximum difference
- Composite XOR and Coprime AND
- Largest number less than or equal to N/2 which is coprime to N
- Finding a Non Transitive Coprime Triplet in a Range
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Divide 1 to n into two groups with minimum sum difference
- Minimum absolute difference between N and a power of 2
- Find the minimum difference between Shifted tables of two numbers
- Find an index such that difference between product of elements before and after it is minimum
- Minimum absolute difference of a number and its closest prime
- Minimum array insertions required to make consecutive difference <= K
- Minimize the difference between the maximum and minimum values of the modified array
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Ways to form n/2 pairs such that difference of pairs is minimum