Given an integer K, the task is to find a pair of numbers (A, B) such that A – B = K and A / B = K. If no such pair can be generated, print “No”.
Examples:
Input: K = 6
Output: 7.2 1.2
Explanation:
Since 7.2 – 1.2 = 6 and 7.2 / 1.2 = 6, The pair {7.2, 1.2} satisfy the necessary conditions.Input: K = 1
Output: No
Approach:
The following observations are needed to be made to solve the problem:
- The given conditions can be written in the form of equations as:
- Equation (1): A – B = K => A – B – K = 0
- Equation (2): A / B = K => A – (K * B) = 0
- Solving both these equations, we obtain:
( K * Equation (1) ) – Equation(2) = 0
=> K*A – K*B – K2 – (A – K*B) = 0
=> K*A – K2 – A – K*B + K*B = 0
=> A*(K-1) – K2 = 0
=> A*(K-1) = K2
=> A = K2 / (K -1)
- Replacing the value of A in Equation (1), the value of B can be obtained to be K / (K – 1)
- It can be observed that if the value of K is 1, then no such pair can be found as the denominator of both A and B becomes 0.
Follow the steps below to solve the problem:
- If K is equal to 1, then print “NO”.
- Otherwise, if K is equal to any value other than 0, compute the values (K*K) / (K -1) and K / (K – 1) and print them.
Below is the implementation of the above approach:
C++
// C++ Program to implement // the above problem #include <bits/stdc++.h> using namespace std; // Function to find the // required pair void computePair(double K) { // No pair possible if (K == 1) { cout << "No"; return; } else { cout << K * K / (K - 1) << " "; cout << K / (K - 1) << endl; } } // Driver Code int main() { double K = 6; computePair(K); return 0; } |
Java
// Java program to implement // the above problem class GFG{ // Function to find the // required pair static void computePair(double K) { // No pair possible if (K == 1) { System.out.print("No"); return; } else { System.out.print(K * K / (K - 1) + " "); System.out.print(K / (K - 1) + "\n"); } } // Driver Code public static void main(String[] args) { double K = 6; computePair(K); } } // This code is contributed by Rajput-Ji |
C#
// C# program to implement // the above problem using System; class GFG{ // Function to find the // required pair static void computePair(double K) { // No pair possible if (K == 1) { Console.Write("No"); return; } else { Console.Write(K * K / (K - 1) + " "); Console.Write(K / (K - 1) + "\n"); } } // Driver Code public static void Main(String[] args) { double K = 6; computePair(K); } } // This code is contributed by gauravrajput1 |
7.2 1.2
Time Complexity: O(1)
Auxiliary 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.
Recommended Posts:
- Pair of integers having difference of their fifth power as X
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Maximum possible GCD for a pair of integers with sum N
- Count of integers in an Array whose length is a multiple of K
- Closest pair in an Array such that one number is multiple of the other
- Print any pair of integers with sum of GCD and LCM equals to N
- Pair of integers (a, b) which satisfy the given equations
- Maximum possible GCD for a pair of integers with product N
- Find pair with maximum GCD for integers in range 2 to N
- Find a pair with maximum product in array of Integers
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Find First element in AP which is multiple of given prime
- Sort integers in array according to their distance from the element K
- K-th smallest element after removing given integers from natural numbers | Set 2
- Count of elements which is product of a pair or an element square
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Minimum pair sum operations to make array each element divisible by 4
- Find N integers with given difference between product and sum
- Pair with min absolute difference and whose product is N+1 or N+2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

