Compare Given Two Powers of 10 in Java

Given A and B, which are denoted as A × 10^Z1 and B × 10^Z2, respectively, as two numbers. The task is to compare these two expressions.

Example 1:

Input: A = 5 × 10^3, B = 2 × 10^4

Output: "A < B"

Explanation: Both A and B have one significant digit. When we multiply, we get a = 5000 and b = 20,000. As a result, we determine A is less than B.

Example 2:

Input: A = 5 × 10^4, B = 3 × 10^4

Output: "A > B"

Explanation: Both A and B have two significant digits. When we multiply, we get a = 50000 and b = 30,000. As a result, we determine A is greater than B.

Using Naive Approach

The naive approach uses integer values `A`, `Z1`, `B,` and `Z2` to represent two pairs of numbers. It calculates the result of multiplying the base value by 10 raised to the power of the corresponding exponent. It then compares the calculated results, printing "A > B" if resultA is greater than resultB, "A < B" if resultA is less than resultB, and "A equals B" if resultA is equal to resultB.

Here's the implementation of comparing given 2 powers of 10 using a naive approach:

FileName: NumberComparator.java

public class NumberComparator {

   // Function to compare 2 numbers A and B in scientific notation

   static String compareScientificNumbers(int coefficientA, int exponentA, int coefficientB, int exponentB) {

     // Adjust the coefficients based on exponents

     int adjustedA = coefficientA * (int) Math.pow(10, exponentA);

     int adjustedB = coefficientB * (int) Math.pow(10, exponentB);

     // Compare the adjusted coefficients

     if (adjustedA == adjustedB) {

       return "=";

     } else if (adjustedA < adjustedB) {

       return "<";

     } else {

       return ">";

     }

   }

   // Driver Code

   public static void main(String args[]) {

     int coefficientA = 5;

     int exponentA = 3; // 5 × 10^3

     int coefficientB = 2;

     int exponentB = 4; // 2 × 10^4

     String result = compareScientificNumbers(coefficientA, exponentA, coefficientB, exponentB);

     System.out.println("A " + result + " B");

   }

}

Output:

A < B

Complexity analysis: The time complexity of a Java program is primarily determined by the calculations of Math.pow(10, Z1) and Math.pow(10, Z2), which are constant-time operations. The time complexity of these operations is O(1), while the space complexity is O(1) for variables and constants used in the program. The space complexity for integer variables and long variables is O(1), while the space complexity for Math.pow is O(1), indicating constant space usage. The overall time and space complexity is O(1), as they are primarily dependent on the Math.pow operation.

Using Optimized Approach

The optimized approach calculates digit counts of two numbers, A and B, using logarithmic operations. It then compares the sum of digits and extra zeros, adjusts if necessary, and performs a standard numerical comparison to determine A's greater, lesser, or equal to B.

Here's the implementation of comparing given 2 powers of 10 using an optimized approach:

FileName: NumberComparator1.java

public class NumberComparator1 {

   // Function to compare 2 numbers A and B

   static String compareNumbers(int a, int b, int z1, int z2) {

     int Adigits = (int) Math.log10(a) + 1;

     int Bdigits = (int) Math.log10(b) + 1;

     // Compare the sum of digits and extra zeros

     if (Adigits + z1 > Bdigits + z2) {

       return ">";

     } else if (Adigits + z1 < Bdigits + z2) {

       return "<";

     }

     // Make the digit count equal

     if (Adigits > Bdigits) {

       b *= Math.pow(10, Adigits - Bdigits);

     } else {

       a *= Math.pow(10, Bdigits - Adigits);

     }

     // Compare the modified numbers

     if (a == b) {

       return "=";

     } else if (a > b) {

       return ">";

     } else {

       return "<";

     }

   }

   // Driver Code

   public static void main(String args[]) {

     int a = 5, z1 = 3;

     int b = 2, z2 = 4;

     String ans = compareNumbers(a, b, z1, z2);

     System.out.println("A " + ans + " B");

   }

}

Output:

A < B

Complexity analysis: The logarithm's time complexity is O(log10(N)), with the overall function time complexity O(log10(a) + log10(b). The optimized approach's space complexity is O(1), as it uses constant additional space, regardless of input size, without scaling data structures.