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 = 6Output:7.2 1.2Explanation:

Since 7.2 – 1.2 = 6 and 7.2 / 1.2 = 6, The pair {7.2, 1.2} satisfy the necessary conditions.

Input:K = 1Output: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**

- Equation (1):
- Solving both these equations, we obtain:

( K * Equation (1) ) – Equation(2) = 0

=> K*A – K*B – K^{2 }– (A – K*B) = 0

=> K*A – K^{2}– A – K*B + K*B = 0

=> A*(K-1) – K^{2 }= 0

=> A*(K-1) = K^{2}

=>**A = K**^{2}/ (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 theof both**denominator****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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

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 least GCD among all given pairs having GCD exceeding K
- Pair of integers having difference of their fifth power as X
- Closest pair in an Array such that one number is multiple of the other
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Smallest subarray having an element with frequency greater than that of other elements
- Multiplication table till N rows where every Kth row is table of K upto Kth term
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Find the Kth pair in ordered list of all possible sorted pairs of the Array
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Length of array pair formed where one contains all distinct elements and other all same elements
- Count of integers in an Array whose length is a multiple of K
- Find the point on X-axis from given N points having least Sum of Distances from all other points
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Queries to calculate the Sum of Array elements in the range [L, R] having indices as multiple of K
- Find a pair with maximum product in array of Integers

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.