Find maximum among x^(y^2) or y^(x^2) where x and y are given
Last Updated :
21 Sep, 2022
Given X and Y whose values are greater than 2, the task is to find out which is maximum among
and
Consider that either
x is greater than
y or
y is greater than
x. So, print 1 if
is greater or 2 if
is greater.
Examples:
Input: X = 4, Y = 9
Output: 1
Input: X = 4, Y = 3
Output: 2
Approach: Assume
, then after taking ln on both sides and dividing by
we can get
.
Take
. This function is monotonically decreasing for
.
If x > y, then F(x) < F(y)
C++
#include <bits/stdc++.h>
using namespace std;
bool findGreater( int x, int y)
{
if (x > y) {
return false ;
}
else {
return true ;
}
}
int main()
{
int x = 4;
int y = 9;
findGreater(x, y) ? cout << "1\n"
: cout << "2\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean findGreater( int x,
int y)
{
if (x > y)
{
return false ;
}
else
{
return true ;
}
}
public static void main (String[] args)
{
int x = 4 ;
int y = 9 ;
if (findGreater(x, y))
System.out.println( "1" );
else
System.out.println( "2" );
}
}
|
Python3
def findGreater(x, y):
if (x > y):
return False ;
else :
return True ;
x = 4 ;
y = 9 ;
if (findGreater(x, y)):
print ( "1" );
else :
print ( "2" );
|
C#
using System;
class GFG
{
static bool findGreater( int x,
int y)
{
if (x > y)
{
return false ;
}
else
{
return true ;
}
}
public static void Main ()
{
int x = 4;
int y = 9;
if (findGreater(x, y))
Console.WriteLine( "1" );
else
Console.WriteLine( "2" );
}
}
|
PHP
<?php
function findGreater( $x , $y )
{
if ( $x > $y )
{
return false;
}
else
{
return true;
}
}
$x = 4;
$y = 9;
if (findGreater( $x , $y ) == true)
echo ( "1\n" );
else
echo ( "2\n" );
?>
|
JavaScript
<script>
function findGreater(x,y)
{
if (x > y)
{
return false ;
}
else
{
return true ;
}
}
var x = 4;
var y = 9;
if (findGreater(x, y))
document.write( "1" );
else
document.write( "2" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...