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
Examples:
Input: N = 6, K = 1
Output: 2 3
Explanation:
Since 2 and 3 are both factors of 6 and gcd(2, 3) = 1.
Input: N = 120, K = 4
Output:
2 3
3 4
3 5
4 5
Naive Approach:
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)
Linear Approach:
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)
Efficient Approach:
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.
C++
#include <bits/stdc++.h>
using namespace std;
void FindPairs( int n, int k)
{
cout << 1 << " " << n << endl;
k--;
for ( long long i = 2;
i <= sqrt (n); i++) {
if (n % i == 0) {
cout << 1 << " "
<< i << endl;
k--;
if (k == 0)
break ;
if (i != n / i) {
cout << 1 << " "
<< n / i << endl;
k--;
}
if (k == 0)
break ;
}
}
}
int main()
{
int N = 100;
int K = 5;
FindPairs(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void FindPairs( int n, int k)
{
System.out.print( 1 + " " + n + "\n" );
k--;
for ( long i = 2 ; i <= Math.sqrt(n); i++)
{
if (n % i == 0 )
{
System.out.print( 1 + " " + i + "\n" );
k--;
if (k == 0 )
break ;
if (i != n / i)
{
System.out.print( 1 + " " +
n / i + "\n" );
k--;
}
if (k == 0 )
break ;
}
}
}
public static void main(String[] args)
{
int N = 100 ;
int K = 5 ;
FindPairs(N, K);
}
}
|
Python3
from math import sqrt
def FindPairs(n, k):
print ( 1 , n)
k - = 1
for i in range ( 2 , int (sqrt(n)) + 1 ):
if (n % i = = 0 ):
print ( 1 , i)
k - = 1
if (k = = 0 ):
break
if (i ! = n / / i):
print ( 1 , n / / i)
k - = 1
if (k = = 0 ):
break
if __name__ = = '__main__' :
N = 100
K = 5
FindPairs(N, K)
|
C#
using System;
class GFG{
static void FindPairs( int n, int k)
{
Console.Write(1 + " " + n + "\n" );
k--;
for ( long i = 2; i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
Console.Write(1 + " " + i + "\n" );
k--;
if (k == 0)
break ;
if (i != n / i)
{
Console.Write(1 + " " +
n / i + "\n" );
k--;
}
if (k == 0)
break ;
}
}
}
public static void Main(String[] args)
{
int N = 100;
int K = 5;
FindPairs(N, K);
}
}
|
Javascript
<script>
function FindPairs(n, k)
{
document.write(1 + " " + n + "<br>" );
k--;
for (let i = 2;
i <= Math.sqrt(n); i++) {
if (n % i == 0) {
document.write( 1 + " "
+ i + "<br>" );
k--;
if (k == 0)
break ;
if (i != n / i) {
document.write(1 + " "
+ n / i + "<br>" );
k--;
}
if (k == 0)
break ;
}
}
}
let N = 100;
let K = 5;
FindPairs(N, K);
</script>
|
Output: 1 100
1 2
1 50
1 4
1 25
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)