Check if there exists some positive integers X and Y such that P^X is equal to Q^Y
Last Updated :
19 Oct, 2022
Given two integers P and Q, the task is to check whether a pair (P, Q) is equal or not, and a pair is said to be equal if there exist some positive integers X and Y such that PX = QY.
Examples:
Input: P = 16 , Q = 4
Output: Yes
?Explanation: Let X = 2 and Y = 4. Thus, PX = 162 = 256 and QY = 44 = 256 . Thus, the pair (16,4) is equal.
Input: P = 12 , Q = 24
Output: No
Approach: The problem can be solved based on the following observation:
For PX = QY to be true for some integer pair (X, Y), any one of the below cases must be true:
- There must exist some integer K, such that
- X = Y = 0
Now to implement this, below algorithm can be used:
- Find maximum(max) and minimum(min) number for two integer.
- Iterate a loop and check if max and min is equal or max is divisible by min, then pair of integer is equal and break from the loop.
- Otherwise, pair of integer is not equal.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void check( int p, int q)
{
int maxi = max(p, q);
int mini = min(p, q);
while ( true ) {
if (maxi == mini) {
cout << "Yes" ;
break ;
}
if (maxi % mini != 0) {
cout << "No" ;
break ;
}
int temp = maxi / mini;
maxi = max(temp, mini);
mini = min(temp, mini);
}
}
int main()
{
int P = 16;
int Q = 4;
check(P, Q);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
public static void check( int p, int q)
{
int max = Math.max(p, q);
int min = Math.min(p, q);
while ( true ) {
if (max == min) {
System.out.println( "Yes" );
break ;
}
if (max % min != 0 ) {
System.out.println( "No" );
break ;
}
int temp = max / min;
max = Math.max(temp, min);
min = Math.min(temp, min);
}
}
public static void main(String[] args)
{
int P = 16 ;
int Q = 4 ;
check(P, Q);
}
}
|
Python3
def check( p, q) :
maxi = max (p, q);
mini = min (p, q);
while ( True ) :
if (maxi = = mini) :
print ( "Yes" );
break ;
if (maxi % mini ! = 0 ) :
print ( "No" );
break ;
temp = maxi / / mini;
maxi = max (temp, mini);
mini = min (temp, mini);
if __name__ = = "__main__" :
P = 16 ;
Q = 4 ;
check(P, Q);
|
C#
using System;
public class GFG{
public static void check( int p, int q)
{
int maxi = Math.Max(p, q);
int mini = Math.Min(p, q);
while ( true ) {
if (maxi == mini) {
Console.WriteLine( "Yes" );
break ;
}
if (maxi % mini != 0) {
Console.WriteLine( "No" );
break ;
}
int temp = maxi / mini;
maxi =Math.Max(temp, mini);
mini =Math.Min(temp, mini);
}
}
static public void Main (){
int P = 16;
int Q = 4;
check(P, Q);
}
}
|
Javascript
function check (p, q)
{
let maxi = Math.max(p, q);
let mini = Math.min(p, q);
while ( true ) {
if (maxi == mini) {
console.log( "Yes" );
break ;
}
if (maxi % mini != 0) {
console.log( "No" );
break ;
}
let temp = Math.floor(maxi / mini);
maxi = Math.max(temp, mini);
mini = Math.min(temp, mini);
}
}
let P = 16;
let Q = 4;
check(P, Q);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...