Find two numbers with difference and division both same as N
Given an integer N, the task is to find two numbers a and b such that a / b = N and a – b = N. Print “No” if no such numbers are possible.
Input: N = 6
a = 7.2
b = 1.2
For the given two numbers a and b, a/b = 6 = N and a-b = 6 = N
Input: N = 1
There are no values of a and b that satisfy the condition.
Approach: To solve the problem observe the equations derived below:
On solving above equations simultaneously, we get:
Since the denominator is N – 1, so the answer will not be possible when N = 1. For all other cases, the answer is possible. Hence find the values of a and b respectively.
Below is the implementation of the above approach:
a = 7.2 b = 1.2
Time Complexity: O(1)
Auxiliary Space: O(1)
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.