Wednesday, July 3, 2024

Find larger of x^y and y^x

Given two integer numbers, X and Y. find the larger of X^Y and Y^X or determine if they are equal.
Examples:  

Input : 2 3
Output : 3^2
We know 3^2 = 9 and 2^3 = 8.

Input : 2 4
Output : Equal

A simple solution is to calculate x^y by looping for y times, but if the values of x and y is too large it will cause an overflow. 
To solve the overflow problem, we can simplify the equation by taking the log. 
log(x^y) = y* log(x) 
Now, this equation will not cause overflow, and we can compare the two values directly.  

C++




// C++ program to print greater of x^y and
// y^x
#include <bits/stdc++.h>
using namespace std;
 
void printGreater(double x, double y)
{
    long double X = y * log(x);
    long double Y = x * log(y);
    if (abs(X - Y) < 1e-9) {
        cout << "Equal";
    }
    else if (X > Y) {
        cout << x << "^" << y;
    }
    else {
        cout << y << "^" << x;
    }
}
 
int main()
{
    double x = 5, y = 8;
    printGreater(x, y);
    return 0;
}


Java




// Java program to print
// greater of x^y and y^x
import java.io.*;
 
class GFG
{
static void printGreater(int x,
                         int y)
{
    double X = y * Math.log(x);
    double Y = x * Math.log(y);
    if (Math.abs(X - Y) < 1e-9)
    {
        System.out.println("Equal");
    }
    else if (X > Y)
    {
        System.out.println(x + "^" + y);
    }
    else
    {
        System.out.println(y + "^" + x);
    }
}
 
// Driver Code
public static void main (String[] args)
{
    int x = 5, y = 8;
    printGreater(x, y);
}
}
 
// This code is contributed
// by anuj_67.


Python3




# Python3 program to print greater
# of x^y and y^x
import math
 
def printGreater(x, y):
 
    X = y * math.log(x);
    Y = x * math.log(y);
    if (abs(X - Y) < 1e-9):
        print("Equal");
    elif (X > Y):
        print(x, "^", y);
    else:
        print(y, "^", x);
 
# Driver Code
x = 5;
y = 8;
printGreater(x, y);
 
# This code is contributed by mits


C#




// C# program to print
// greater of x^y and y^x
using System;
 
class GFG
{
static void printGreater(int x,
                          int y)
{
    double X = y * Math.Log(x);
    double Y = x * Math.Log(y);
    if (Math.Abs(X - Y) < 1e-9)
    {
        Console.WriteLine("Equal");
    }
    else if (X > Y)
    {
        Console.WriteLine(x +
                          "^" + y);
    }
    else
    {
        Console.WriteLine(y +
                          "^" + x);
    }
}
 
// Driver Code
public static void Main ()
{
    int x = 5, y = 8;
    printGreater(x, y);
}
}
 
// This code is contributed
// by anuj_67.


PHP




<?php
// PHP program to print greater
// of x^y and y^x
function printGreater($x, $y)
{
    $X = $y * log($x);
    $Y = $x * log($y);
    if (abs($X - $Y) < 1e-9)
    {
        echo "Equal";
    }
    else if ($X > $Y)
    {
        echo $x . "^" . $y;
    }
    else
    {
        echo $y . "^" . $x;
    }
}
 
// Driver Code
$x = 5;
$y = 8;
printGreater($x, $y);
 
// This code is contributed by mits
?>


Javascript




<script>
 
// Javascript program to print greater of x^y and
// y^x
 
 
function printGreater(x, y)
{
    let X = y * Math.log(x);
    let Y = x * Math.log(y);
    if (Math.abs(X - Y) < 1e-9) {
        document.write("Equal");
    }
    else if (X > Y) {
        document.write(x + "^" + y);
    }
    else {
        document.write(y + "^" + x);
    }
}
 
// Driver Code
 
let x = 5, y = 8;
printGreater(x, y);
 
 
</script>


Output: 

5^8

 

Time complexity: O(1) because constant operations are being performed
Auxiliary space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Ted Musemwa
As a software developer I’m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments