Given two integers N and K, the task is to find K pair of factors of the number N such that the GCD of each pair of factors is 1.
Note: K co-prime factors always exist for the given number
Input: N = 6, K = 1
Output: 2 3
Since 2 and 3 are both factors of 6 and gcd(2, 3) = 1.
Input: N = 120, K = 4
The simplest approach would be to check all the numbers upto N and check if the GCD of the pair is 1.
Time Complexity: O(N2)
Space Complexity: O(1)
Find all possible divisors of N and store in another array. Traverse through the array to search for all possible coprime pairs from the array and print them.
Time Complexity: O(N)
Space Complexity: O(N)
Follow the steps below to solve the problem:
- It can be observed that if GCD of any number, say x, with 1 is always 1, i.e. GCD(1, x) = 1.
- Since 1 will always be a factor of N, simply print any K factors of N with 1 as the coprime pairs.
Below is the implementation of the above approach.
1 100 1 2 1 50 1 4 1 25
Time Complexity: O(sqrt(N))
Auxilairy Space: 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.
- Count all pairs of divisors of a number N whose sum is coprime with N
- Find number of factors of N when location of its two factors whose product is N is given
- Check if all the pairs of an array are coprime with each other
- Maximum number of prime factors a number can have with exactly x factors
- Check if a number exists having exactly N factors and K prime factors
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Largest number less than or equal to N/2 which is coprime to N
- Partition first N natural number into two sets such that their sum is not coprime
- 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
- 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
- Count of integers up to N which are non divisors and non coprime with N
- Program to print factors of a number in pairs
- Number of factors of very large number N modulo M where M is any prime number
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.