Print any pair of integers with sum of GCD and LCM equals to N
Last Updated :
01 Nov, 2021
Given an integer N, the task is to print any pair of integers that have the sum of GCD and LCM equal to N.
Examples:
Input: N = 14
Output: 1, 13
Explanation:
For the given pair we have GCD(1, 13) = 1 and LCM (1, 13) = 13. Sum of GCD and LCM = 1 + 13 = 14.
Input: 2
Output: 1 1
Explanation:
For the given pair we have GCD(1, 1) = 1 and LCM (1, 1) = 1. Sum of GCD and LCM = 1 + 1 = 2.
Approach:
To solve the problem mentioned above let us consider the pair to be (1, n-1). GCD of (1, n-1) = 1 and LCM of (1, n-1) = n – 1. So the sum of GCD and LCM = 1 + (n – 1) = n. Hence the pair (1, n – 1) will be the pair which has the sum of GCD and LCM equal to N.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printPair( int n)
{
cout << 1 << " " << n - 1;
}
int main()
{
int n = 14;
printPair(n);
return 0;
}
|
Java
class GFG{
static void printPair( int n)
{
System.out.print( 1 + " " + (n - 1 ));
}
public static void main(String[] args)
{
int n = 14 ;
printPair(n);
}
}
|
Python3
def printPair(n):
print ( "1" , end = " " )
print (n - 1 )
n = 14
printPair(n)
|
C#
using System;
public class GFG{
static void printPair( int n)
{
Console.Write(1 + " " + (n - 1));
}
public static void Main(String[] args)
{
int n = 14;
printPair(n);
}
}
|
Javascript
<script>
function printPair(n)
{
document.write(1 + " " + (n - 1));
}
var n = 14;
printPair(n);
</script>
|
Time Complexity:O(1)
Auxiliary Space:O(1)
Share your thoughts in the comments
Please Login to comment...