

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
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)
Explanation:
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,
Example
#include <iostream> using namespace std; bool checkGreaterVal(int x, int y) { if (x > y) return false; else return true; } int main() { int x = 3; int y = 5; cout<<"The greater value is "; if(checkGreaterVal(x, y)) cout<<x<<"^("<<y<<"^2)"; else cout<<y<<"^("<<x<<"^2)"; return 0; }
Output
The greater value is 3^(5^2)
- Related Questions & Answers
- Find larger of x^y and y^x in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Find the value of the function Y = (X^6 + X^2 + 9894845) % 981 in C++
- Find value of y mod (2 raised to power x) in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Check if a number can be expressed as 2^x + 2^y in C++
- Evaluate a 2-D polynomial at points (x, y) in Python
- Count of pairs (x, y) in an array such that x < y in C++
- MySQL where column = 'x, y, z'?
- Evaluate a 2-D Chebyshev series at points (x, y) in Python
- Evaluate a 2-D Hermite series at points (x,y) in Python
- Evaluate a 2-D Hermite_e series at points (x,y) in Python
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*y < n in C++