Open In App

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

Last Updated : 22 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


Python3




# Python3 program to implement
# the above problem
 
# Function to find the
# required pair
def computePair(K):
     
    # No pair possible
    if (K == 1):
        print("No")
        return
 
    else:
        print(K * K / (K - 1), end = " ")
        print(K / (K - 1))
 
# Driver Code
if __name__ == "__main__":
 
    K = 6
     
    computePair(K)
 
# This code is contributed by chitranayal


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


Javascript




<script>
 
// Javascript Program to implement
// the above problem
 
// Function to find the
// required pair
function computePair(K)
{
    // No pair possible
    if (K == 1) {
        document.write( "No");
        return;
    }
    else {
 
        document.write( K * K / (K - 1) + " ");
        document.write( K / (K - 1) );
    }
}
 
// Driver Code
var K = 6;
computePair(K);
 
// This code is contributed by rutvik_56.
</script>


Output: 

7.2 1.2

 

Time Complexity: O(1) 
Auxiliary Space; O(1)
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads