VOOZH about

URL: https://www.geeksforgeeks.org/dsa/compare-given-two-powers-of-10/

⇱ Compare given two powers of 10 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Compare given two powers of 10

Last Updated : 23 Jul, 2025

Given 4 integers A, B, Z1, and Z2. The task is to compare A*10Z1 and B*10Z2. 

Examples:

Input: A = 19, Z1 = 2, B = 20, Z2 = 1
Output: A > B
Explanation:
A can be written as 1900 
B can be written as 200 
So, A is greater than B.

Input:, A = 199, Z1 =10, B = 96, Z2 = 1000
Output: A < B
Explanation:
A can be written as 19900000.... 
B can be written as 9600000......
So, A is smaller than B

Naive Approach : Multiply A with Z1 zeroes and B with Z2 zeroes and compare both But large number cannot be store in long long integer more than 18 digits.
Time Complexity: O(1)
Auxiliary Space: O(1)

Efficient Approach: The idea is to compare the total number of digits in A and B because the largest digit number is maximum than the other.

  • Take two variables and adigits and bdigits and initialize to zero.
  • Initialize the variables tempA and tempB as A and B and traverse in the while loop and store the number of digits in A and B.
  • Compare the values of adigits+z1 and bdigits+z2. If their values are equal, then perform the following tasks:
    • If adigits is greater than bdigits, then initialize the variables addZeros as adigits-bdigits and b as 10addZeros and vice-versa if bdigits is greater than adigits.
  • Now, compare the values of a and b, perform the results.

Below is the implementation of the above approach.

 
 


Output
A = B


 

Time Complexity: O(1)
Auxiliary Space: O(1)


 

Comment
Article Tags: