Pair of integers with difference K having an element as the K-th multiple of the other

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: 
    1. Equation (1): A – B = K => A – B – K = 0
    2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Rajput-Ji, GauravRajput1