Given an integer N, the task is to print any pair of integers that have the sum of GCD and LCM equal to N.
Input: N = 14
Output: 1, 13
For the given pair we have GCD(1, 13) = 1 and LCM (1, 13) = 13. Sum of GCD and LCM = 1 + 13 = 14.
Output: 1 1
For the given pair we have GCD(1, 1) = 1 and LCM (1, 1) = 1. Sum of GCD and LCM = 1 + 1 = 2.
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:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.