VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-larger-xy-yx/

⇱ Find larger of x^y and y^x - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find larger of x^y and y^x

Last Updated : 19 Dec, 2022

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.  


Output: 
5^8

 

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

Comment