Given two integer X and Y, the task is compare XY and YX for large values of X and Y.
Examples:
Input: X = 2, Y = 3
Output: 2^3 < 3^2
23 < 32
Input: X = 4, Y = 5
Output: 4^5 > 5^4
Naive approach: A basic approach is to find the values XY and YX and compare them which can overflow as the values of X and Y can be large
Better approach: Taking log of both the equations, log(XY) = Y * log(X) and log(YX) = X * log(Y). Now, these values can be compared easily without overflows.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std;
// Function to compare x^y and y^x void compareVal( int x, int y)
{ // Storing values OF x^y AND y^x
long double a = y * log (x);
long double b = x * log (y);
// Comparing values
if (a > b)
cout << x << "^" << y << " > "
<< y << "^" << x;
else if (a < b)
cout << x << "^" << y << " < "
<< y << "^" << x;
else if (a == b)
cout << x << "^" << y << " = "
<< y << "^" << x;
} // Driver code int main()
{ long double x = 4, y = 5;
compareVal(x, y);
return 0;
} |
// Java implementation of the approach import java.util.*;
class GFG
{ // Function to compare x^y and y^x static void compareVal( int x, int y)
{ // Storing values OF x^y AND y^x
double a = y * Math.log(x);
double b = x * Math.log(y);
// Comparing values
if (a > b)
System.out.print(x + "^" + y + " > " +
y + "^" + x);
else if (a < b)
System.out.print(x + "^" + y + " < " +
y + "^" + x);
else if (a == b)
System.out.print(x + "^" + y + " = " +
y + "^" + x );
} // Driver code public static void main(String[] args)
{ int x = 4 , y = 5 ;
compareVal(x, y);
} } // This code is contributed by 29AjayKumar |
# Python3 implementation of the approach from math import log
# Function to compare x^y and y^x def compareVal(x, y) :
# Storing values OF x^y AND y^x
a = y * log(x);
b = x * log(y);
# Comparing values
if (a > b) :
print (x, "^" , y, ">" , y, "^" , x);
elif (a < b) :
print (x, "^" , y, "<" , y , "^" , x);
elif (a = = b) :
print (x, "^" , y, "=" , y, "^" , x);
# Driver code if __name__ = = "__main__" :
x = 4 ; y = 5 ;
compareVal(x, y);
# This code is contributed by AnkitRai01 |
// C# implementation of the approach using System;
class GFG
{ // Function to compare x^y and y^x static void compareVal( double x, double y)
{ // Storing values OF x^y AND y^x
double a = y * Math.Log(x);
double b = x * Math.Log(y);
// Comparing values
if (a > b)
Console.Write (x + "^" + y + " > " +
y + "^" + x);
else if (a < b)
Console.Write (x + "^" + y + " < " +
y + "^" + x);
else if (a == b)
Console.Write (x + "^" + y + " = " +
y + "^" + x );
} // Driver code static public void Main ()
{ double x = 4, y = 5;
compareVal(x, y);
} } // This Code is contributed by ajit. |
<script> // Javascript implementation of the approach // Function to compare x^y and y^x function compareVal(x, y)
{ // Storing values OF x^y AND y^x
let a = y * Math.log(x);
let b = x * Math.log(y);
// Comparing values
if (a > b)
document.write(x + "^" + y + " > "
+ y + "^" + x);
else if (a < b)
document.write(x + "^" + y + " < "
+ y + "^" + x);
else if (a == b)
document.write(x + "^" + y + " = "
+ y + "^" + x);
} // Driver code let x = 4, y = 5;
compareVal(x, y);
</script> |
4^5 > 5^4
Time Complexity: O(1)
Auxiliary Space: O(1)