Given three integers A, B, and C, the task is to find out a positive integer K (if exists) such that if k is multiplied by any of the given integers A, B, or C they form G.P.(Geometric Progression) in the given order. If there exists no K print -1.
Examples:
Input: A = 1, B = 9, C = 27
Output: 3
Explanation: If A is multiplied by 3 then the integers 3, 9, and 27 forms a G.P. with a common ratio of 3.Input: A = 3, B = 2, C = 12
Output: 3
Explanation: If B is multiplied by 3 then the integers 3, 6, and 12 form a G.P. with a common ratio of 2.Input: A = 2, B = 6, C = 8
Output: -1
Explanation: There exists no positive integer K
Approach: Implement the idea below to solve the problem:
Three numbers A, B, and C form a G.P. if and only if C / B = B / A = r. ( where r is the common ratio ). For three integers we have 3 cases:
- Case 1: A is multiplied by K, Now the integers become A*K, B, and C. Using the G.P. property we have C / B = B / A*K => K = ( B*B ) / ( A*C )
- Case 2: B is multiplied by K, Now the integers become A, B*K, and C. Using the G.P. property we have C / B*k = B*K / A => K = sqrt(A*C)/ B
- Case 3: C is multiplied by K, Now the integers become A, B, and C*K. Using the G.P. property we have C / B = B / A*K => K = ( B*B ) / ( A*C )
If any of the above 3 three cases yield a positive integer K then the answer(s) is/are found else return -1.
Follow these steps to solve the above problem:
- Check the cases mentioned above.
- If any of the cases return true, Print respectively K value for that case.
- Otherwise, return -1.
Below is the implementation of the above approach:
// C++ code for the given approach #include <cmath> #include <iostream> using namespace std;
// Utility function to check if given a number is a // positive integer bool ifposInt( double num)
{ int a = ( int )num;
if (num - a > 0) {
return false ;
}
else {
return true ;
}
} // Function to find any integer M if exists void findk( int A, int B, int C)
{ double k1 = ( double )((B * B) / (A * C));
double k2 = ( double )( sqrt (C * A) / B);
double k3 = ( double )((B * B) / (A * C));
// Checks if possible values k are positive
// integers or not
if (k1 > 1 && ifposInt(k1)) {
cout << ( int )k1 << endl;
}
else if (k2 > 1 && ifposInt(k2)) {
cout << ( int )k2 << endl;
}
else if (k3 > 1 && ifposInt(k3)) {
cout << ( int )k3 << endl;
}
else {
cout << "-1" << endl;
}
} int main()
{ int A = 3;
int B = 2;
int C = 12;
// Function call
findk(A, B, C);
return 0;
} // This code is contributed by lokeshmvs21. |
// Java code for the given approach import java.io.*;
class GFG {
// Utility function to check if given a number is a
// positive integer
static boolean ifposInt( double num)
{
int a = ( int )num;
if (num - a > 0 ) {
return false ;
}
else {
return true ;
}
}
// Function to find any integer M if exists
static void findk( int A, int B, int C)
{
double k1 = ( double )((B * B) / (A * C));
double k2 = ( double )(Math.sqrt(C * A) / B);
double k3 = ( double )((B * B) / (A * C));
// Checks if possible values k are positive
// integers or not
if (k1 > 1 && ifposInt(k1)) {
System.out.println(( int )k1);
}
else if (k2 > 1 && ifposInt(k2)) {
System.out.println(( int )k2);
}
else if (k3 > 1 && ifposInt(k3)) {
System.out.println(( int )k3);
}
else {
System.out.println( "-1" );
}
}
public static void main(String[] args)
{
int A = 3 ;
int B = 2 ;
int C = 12 ;
// Function call
findk(A, B, C);
}
} // This code is contributed by lokesh. |
# Python3 code for the given approach import math
# Utility function to check if given a # number is a positive integer def ifposInt(num):
a = int (num)
if num - a > 0 :
return False
else :
return True
# Function to find any integer # M if exists def findk(A, B, C):
k1 = (B * B) / (A * C)
k2 = math.sqrt(C * A) / B
k3 = (B * B) / (A * C)
# Checks if possible values k are
# positive integers or not
if k1 > 1 and ifposInt(k1):
k1 = int (k1)
print (k1)
elif k2 > 1 and ifposInt(k2):
k2 = int (k2)
print (k2)
elif k3 > 1 and ifposInt(k3):
k3 = int (k3)
print (k3)
else :
print ( - 1 )
# Driver code A = 3
B = 2
C = 12
#Function call findk(A, B, C) #This code is contributed by nikhilsainiofficial546 |
// C# code for the given approach using System;
class MainClass {
// Utility function to check if given a
// number is a positive integer
static bool ifposInt( double num)
{
int a = ( int )num;
if (num - a > 0)
return false ;
else
return true ;
}
// Function to find any integer
// M if exists
static void findk( int A, int B, int C)
{
double k1 = ( double )((B * B) / (A * C));
double k2 = ( double )(Math.Sqrt(C * A) / B);
double k3 = ( double )((B * B) / (A * C));
// Checks if possible values k are
// positive integers or not
if (k1 > 1 && ifposInt(k1))
Console.WriteLine(k1);
else if (k2 > 1 && ifposInt(k2))
Console.WriteLine(k2);
else if (k3 > 1 && ifposInt(k3))
Console.WriteLine(k3);
else
Console.WriteLine( "-1" );
}
//Drive Code
public static void Main( string [] args)
{
int A = 3;
int B = 2;
int C = 12;
// Function call
findk(A, B, C);
}
} // This code is contributed by nikhilsainiofficial546 |
// Javascript code for the given approach // Utility function to check if given a number is a // positive integer function ifposInt(num)
{ let a = parseInt(num);
if (num - a > 0) {
return false ;
}
else {
return true ;
}
} // Function to find any integer M if exists function findk( A, B, C)
{ let k1 = ((B * B) / (A * C));
let k2 = (Math.sqrt(C * A) / B);
let k3 = ((B * B) / (A * C));
// Checks if possible values k are positive
// integers or not
if (k1 > 1 && ifposInt(k1)) {
console.log(parseInt(k1));
}
else if (k2 > 1 && ifposInt(k2)) {
console.log(parseInt(k2));
}
else if (k3 > 1 && ifposInt(k3)) {
console.log(parseInt(k3));
}
else {
console.log( "-1" );
}
} let A = 3;
let B = 2;
let C = 12;
// Function call
findk(A, B, C);
// This code is contributed by poojaagarwal2.
|
3
Time Complexity: O(1)
Auxiliary Space: O(1)