Co-prime or mutually prime pair are those pair of numbers whose GCD is 1. Given an array of size n, find number of Co-Prime or mutually prime pairs in the array.
Input : 1 2 3 Output : 3 Here, Co-prime pairs are ( 1, 2), ( 2, 3), ( 1, 3) Input :4 8 3 9 Output :4 Here, Co-prime pairs are ( 4, 3), ( 8, 3), ( 4, 9 ), ( 8, 9 )
Approach : Using two loop, check every possible pair of the array. If Gcd of the pair is 1 increment counter value and at last display it.
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 firstname.lastname@example.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.
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.
- Check if all the pairs of an array are coprime with each other
- Count all pairs of divisors of a number N whose sum is coprime with N
- Construct an array of first N natural numbers such that every adjacent pair is coprime
- Largest number less than or equal to N/2 which is coprime to N
- Coprime divisors of a number
- Finding a Non Transitive Coprime Triplet in a Range
- 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
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Print all distinct Coprime sets possible from 1 to N
- Count of integers up to N which are non divisors and non coprime with N
- Print all Coprime path of a Binary Tree
- Ways to form n/2 pairs such that difference of pairs is minimum
- Find pairs in array whose sum does not exist in Array
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Count all distinct pairs of repeating elements from the array for every array element
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum