![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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)