Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++

C++Server Side ProgrammingProgramming

In this problem, we are given two values x and y. Our task is to find maximum among x^(y^2) or y^(x^2) where x and y are given. 

Let’s take an example to understand the problem, 

Input: x = 4, y = 3

Output: 3^(4^2)


x^(y^2) = 4^(3^2) = 4^9 = 262144
y^(x^2) = 3^(4^2) = 3^16 = 43046721

Solution approach

One approach can be to calculate both values and then print the maximum of both. But this method does not work when the values are large.

A simple and easy approach is using natural log (ln) which will be the solution easier.

ln(x^(y^2)) = (y^2) * ln(x)

ln(y^(x^2)) = (x^2) * ln(y)

Here, the values are not directly proportional to x and y. So, let’s divide the values by (x^2)*(y^2). This makes the value,

ln(x^(y^2)) / (x^2)*(y^2) = ln(x) / (x^2)

ln(y^(x^2)) / (x^2)*(y^2) = ln(y)/ (y^2)

These values are inversely proportional to the resulting value.

If x > y, then x^(y^2) < y^(x^2)

Program to illustrate the working of our solution,


Live Demo

#include <iostream>
using namespace std;

bool checkGreaterVal(int x, int y) {
   if (x > y)
      return false;
      return true;

int main() {
   int x = 3;
   int y = 5;
   cout<<"The greater value is ";
   if(checkGreaterVal(x, y))
   return 0;


The greater value is 3^(5^2)
Published on 25-Jan-2021 09:50:13