Given four integers a, b, m, and n. Compute the values ab and mn. Return 1 if ab is greater, 0 if mn is greater, or -1 if both values are equal.
Examples:
Input: a = 2, b = 2, m = 3, n = 2
Output: 0
Explanation: 22 = 4, and 32 = 9. Since 4 < 9 the output is 0.Input: a = 1 , b = 23 , m = 1 , n = 989
Output: -1
Explanation: Both 123 and 1989 = 1. So, the Output is -1.
Using Logarithmic Approach - O(1) Time and O(1) Space
A straightforward brute-force approach would be to calculate both powers and then compare them. However, this may not work when the exponents are large, because even when a, b, m, and n individually fit easily inside an integer, the values of ab and mn can become extremely large and cause integer overflow. Using repeated multiplication also takes unnecessary time.
Instead of calculating the actual powers, we use logarithm which is a strictly increasing function for positive numbers. Therefore, if ab > mn, then taking logarithm on both sides preserves the comparison:
log(ab) > log(mn)
Using the logarithm property log(xy) = y * log(x) we get:
b * log(a) > n * log(m)
Therefore, we don't need to calculate the huge values ab and mn at all. We only calculate the two much smaller quantities: b * log(a) and n * log(m). Then:
- If b * log(a) > n * log(m), return 1.
- If b * log(a) < n * log(m), return 0.
- If they are equal, return -1.
- Handle base 0 separately since log(0) is undefined.
- Compute first = b × log(a) and second = n × log(m).
- Compare these values instead of directly calculating ab and mn.
- If their difference is within a small tolerance, return -1.
- If first > second, return 1; otherwise, return 0.
#include <bits/stdc++.h>
using namespace std;
long double powerLog(int base, int exp)
{
return (long double)exp * logl((long double)base);
}
int comparePowers(int a, int b, int m, int n)
{
// Handle base = 0 separately because log(0) is undefined.
// Assuming the exponents are positive.
if (a == 0 && m == 0)
return -1;
if (a == 0)
return 0;
if (m == 0)
return 1;
// log(a^b) = b * log(a)
// log(m^n) = n * log(m)
long double first = powerLog(a, b);
long double second = powerLog(m, n);
// Due to floating-point precision, two mathematically
// equal values may differ by a very small amount.
const long double eps = 1e-12L;
// Check whether the two logarithmic values are effectively equal.
if (fabsl(first - second) <= eps)
return -1;
// Compare the logarithmic values.
if (first > second)
return 1;
return 0;
}
int main()
{
int a = 2, b = 2;
int m = 3, n = 2;
cout << comparePowers(a, b, m, n) << endl;
return 0;
}
public class GFG {
static double powerLog(int base, int exp)
{
return (double)exp * Math.log(base);
}
static int comparePowers(int a, int b, int m, int n)
{
// Handle base = 0 separately because log(0) is
// undefined. Assuming the exponents are positive.
if (a == 0 && m == 0)
return -1;
if (a == 0)
return 0;
if (m == 0)
return 1;
// log(a^b) = b * log(a)
// log(m^n) = n * log(m)
double first = powerLog(a, b);
double second = powerLog(m, n);
// Due to floating-point precision, two
// mathematically equal values may differ by a very
// small amount.
double eps = 1e-12;
// Check whether the two logarithmic values are
// effectively equal.
if (Math.abs(first - second) <= eps)
return -1;
// Compare the logarithmic values.
if (first > second)
return 1;
return 0;
}
public static void main(String[] args)
{
int a = 2, b = 2;
int m = 3, n = 2;
System.out.println(comparePowers(a, b, m, n));
}
}
import math
def powerLog(base, exp):
# Returns b * log(a).
# We use logarithms instead of calculating a^b directly
# because a^b can become extremely large.
return exp * math.log(base)
def comparePowers(a, b, m, n):
# Handle base = 0 separately because log(0) is undefined.
# Assuming the exponents are positive.
if a == 0 and m == 0:
return -1
if a == 0:
return 0
if m == 0:
return 1
# log(a^b) = b * log(a)
# log(m^n) = n * log(m)
first = powerLog(a, b)
second = powerLog(m, n)
# Due to floating-point precision, two mathematically
# equal values may differ by a very small amount.
eps = 1e-12
# Check whether the two logarithmic values are effectively equal.
if abs(first - second) <= eps:
return -1
# Compare the logarithmic values.
if first > second:
return 1
return 0
# Driver Code
if __name__ == "__main__":
a, b = 2, 2
m, n = 3, 2
print(comparePowers(a, b, m, n))
using System;
class GFG {
static double powerLog(int baseValue, int exp)
{
return (double)exp * Math.Log(baseValue);
}
static int comparePowers(int a, int b, int m, int n)
{
// Handle base = 0 separately because log(0) is
// undefined. Assuming the exponents are positive.
if (a == 0 && m == 0)
return -1;
if (a == 0)
return 0;
if (m == 0)
return 1;
// log(a^b) = b * log(a)
// log(m^n) = n * log(m)
double first = powerLog(a, b);
double second = powerLog(m, n);
// Due to floating-point precision, two
// mathematically equal values may differ by a very
// small amount.
double eps = 1e-12;
// Check whether the two logarithmic values are
// effectively equal.
if (Math.Abs(first - second) <= eps)
return -1;
// Compare the logarithmic values.
if (first > second)
return 1;
return 0;
}
static void Main()
{
int a = 2, b = 2;
int m = 3, n = 2;
Console.WriteLine(comparePowers(a, b, m, n));
}
}
function powerLog(base, exp)
{
// Returns b * log(a).
// We use logarithms instead of calculating a^b directly
// because a^b can become extremely large.
return exp * Math.log(base);
}
function comparePowers(a, b, m, n)
{
// Handle base = 0 separately because log(0) is
// undefined. Assuming the exponents are positive.
if (a === 0 && m === 0)
return -1;
if (a === 0)
return 0;
if (m === 0)
return 1;
// log(a^b) = b * log(a)
// log(m^n) = n * log(m)
const first = powerLog(a, b);
const second = powerLog(m, n);
// Due to floating-point precision, two mathematically
// equal values may differ by a very small amount.
const eps = 1e-12;
// Check whether the two logarithmic values are
// effectively equal.
if (Math.abs(first - second) <= eps)
return -1;
// Compare the logarithmic values.
if (first > second)
return 1;
return 0;
}
// Driver Code
const a = 2, b = 2;
const m = 3, n = 2;
console.log(comparePowers(a, b, m, n));
Output
0