Solved Problem - Compare Numbers

In this lesson, we'll discuss another string manipulation problem.

Problem statement

Given two very long integers AA and BB. You have to determine which is bigger or if they are equal. Leading zeroes are allowed.

Input format

The first line contains a non-negative integer, AA.

The second line contains a non-negative integer, BB.

AA,BB may contain leading zeroes. Each of them consists of no more than 10610^6 digits.

Output format

Print a single character:

  • A - if A>BA > B
  • B - if B>AB > A
  • = - If numbers are equal

Sample

Input 1

6 
19

Output 1

B

Input 2

23567
0023534

Output 2

A

Input 3

63
0063

Output 3

=

Explanation

Sample 2: The numbers without leading zeros are 23 and 12. We’ll print A since 23>1223 > 12.


Solution

First of all, the number of digits can be 10610^6. So clearly, no integer data type is enough (int is 10 digits). We have to process the input as a string.

Before comparing the string, we remove the trailing zeros from both the numbers.

Comparing two numbers as a string without leading zeros has three cases:

  1. len(A)>len(B)len(A) > len(B) - A is larger
  2. len(A)<len(B)len(A) < len(B) - B is larger
  3. len(A)=len(B)len(A) = len(B) - First digit from the left that is different. If all digits are same, print =.

Removing leading zeros

Removing the first character one by one in order to remove the leading zeroes will be an O(N2)O(N^2) operation. A better way to do this would be to create a new string without leading zeros.

This would remove leading zeros in O(N)O(N).

Get hands-on with 1200+ tech skills courses.