Given three integers a, b and n, the task is to find out greater value between an and bn.
Examples:
Input: a = 3, b = 4, n = 5
Output: b^n is greater than a^n
Value of an is 243 and the value of bn is 1024.
So, bn is greater than an.
Input: a = -3, b = 2, n = 4
Output: a^n is greater than b^n
Value of an is 243 and the value of bn is 16.
So, an is greater than bn.
Basic Approach: For every value of a, b and n, calculate the values of an and bn. Then compare the result obtained and display the according to output.
The problem with this approach arises when there are large values of a, b and n. For large values of a, n, calculating an can exceed the limit of integer which will cause integer overflow.
Better approach is to check the value of n.
- If n is even then calculate the absolute value of a and b.
- If n is odd then take the given value as it is.
- Now check if a is equal to b. If yes, print 0.
- If a is greater than b, print 1.
- Otherwise, print 2.
C++
#include <bits/stdc++.h>
using namespace std;
void findGreater( int a, int b, int n)
{
if (!(n & 1)) {
a = abs (a);
b = abs (b);
}
if (a == b)
cout << "a^n is equal to b^n" ;
else if (a > b)
cout << "a^n is greater than b^n" ;
else
cout << "b^n is greater than a^n" ;
}
int main()
{
int a = 12, b = 24, n = 5;
findGreater(a, b, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findGreater( int a,
int b, int n)
{
if (!((n & 1 ) > 0 ))
{
a = Math.abs(a);
b = Math.abs(b);
}
if (a == b)
System.out.println( "a^n is " +
"equal to b^n" );
else if (a > b)
System.out.println( "a^n is greater " +
"than b^n" );
else
System.out.println( "b^n is greater " +
"than a^n" );
}
public static void main (String[] args)
{
int a = 12 , b = 24 , n = 5 ;
findGreater(a, b, n);
}
}
|
Python3
import math
def findGreater(a, b, n):
if ((n & 1 ) > 0 ):
a = abs (a);
b = abs (b);
if (a = = b):
print ( "a^n is equal to b^n" );
elif (a > b):
print ( "a^n is greater than b^n" );
else :
print ( "b^n is greater than a^n" );
a = 12 ;
b = 24 ;
n = 5 ;
findGreater(a, b, n);
|
C#
using System;
class GFG
{
static void findGreater( int a,
int b,
int n)
{
if (!((n & 1) > 0))
{
a = Math.Abs(a);
b = Math.Abs(b);
}
if (a == b)
Console.WriteLine( "a^n is " +
"equal to b^n" );
else if (a > b)
Console.WriteLine( "a^n is greater " +
"than b^n" );
else
Console.WriteLine( "b^n is greater " +
"than a^n" );
}
public static void Main()
{
int a = 12, b = 24, n = 5;
findGreater(a, b, n);
}
}
|
PHP
<?php
function findGreater( $a , $b , $n )
{
if (!( $n & 1))
{
$a = abs ( $a );
$b = abs ( $b );
}
if ( $a == $b )
echo "a^n is equal to b^n" ;
else if ( $a > $b )
echo "a^n is greater than b^n" ;
else
echo "b^n is greater than a^n" ;
}
$a = 12; $b = 24; $n = 5;
findGreater( $a , $b , $n );
?>
|
Javascript
<script>
function findGreater(a , b , n)
{
if (!((n & 1) > 0)) {
a = Math.abs(a);
b = Math.abs(b);
}
if (a == b)
document.write( "a^n is " + "equal to b^n" );
else if (a > b)
document.write( "a^n is greater " + "than b^n" );
else
document.write( "b^n is greater " + "than a^n" );
}
var a = 12, b = 24, n = 5;
findGreater(a, b, n);
</script>
|
Output: b^n is greater than a^n
Time Complexity:O(1)
Auxiliary Space: O(1)