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 .
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.
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 email@example.com. 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.
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.
- Count all pairs of divisors of a number N whose sum is coprime with N
- Finding a Non Transitive Coprime Triplet in a Range
- Largest number less than or equal to N/2 which is coprime to N
- Composite XOR and Coprime AND
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Largest Coprime Set Between two integers
- Print all Coprime path of a Binary Tree
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Print all distinct Coprime sets possible from 1 to N
- Coprime divisors of a number
- Count of integers up to N which are non divisors and non coprime with N
- Check if all the pairs of an array are coprime with each other
- Pair of prime numbers with a given sum and minimum absolute difference
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Minimum Sum of a pair at least K distance apart from an Array
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Split array into minimum number of subsets having maximum pair sum at most K
- Minimum distance between any special pair in the given array
- Pair having all other given pairs lying between its minimum and maximum
- Product of absolute difference of every pair in given Array