Compare Two Integers: Difference between revisions
imported>Hss9 Created page with "==Introduction== You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal. Th..." |
imported>Kmk21 No edit summary |
||
Line 71: | Line 71: | ||
[[Category:Algorithm Easy]] | [[Category:Algorithm Easy]] | ||
[[Category:Implementation Easy]] | [[Category:Implementation Easy]] | ||
[[Category:Big Numbers]] |
Latest revision as of 05:51, 24 August 2016
Introduction
You are given two very long integers a, b (leading zeroes are allowed). You should check what number a or b is greater or determine that they are equal.
The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input().
Solutions
Idea
This problem cannot be solved simply using "parseInt" and compareTo to return the greater integer, because the numbers can be too big for Java to process using Integer. So I went about solving the problem with the simplest way I could think of, which would be to compare the length of the two integers. Of course, this is after the two integers are processed to remove any leading zeroes to make sure the test of checking their lengths works and does not take the leading zeroes as actual digit-values. If one number is longer than another, it is automatically larger in value than the other one. If the two integers are the same length, I iterate through each digit and find the first point of difference. Because I start at the largest digit, the first point of difference will yield which integer is larger, without having to consider any subsequent digits (they are all smaller in value than the one currently being compared).
Runtime
Worst case runtime is O(n), where n is the length of the longer integer.
Code
Solution - Java
import java.util.Scanner;
/**
*
* @author Created by harrisonsuh
*
*/
public class CompareTwoIntegers {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.next();
String b = in.next();
//remove any leading zeroes from first int
for (int i = 0; i < a.length(); i++) {
if (Integer.parseInt(a.substring(i, i+1)) != 0 || i+1 == a.length()) {
a = a.substring(i);
break;
}
}
//remove any leading zeroes for second int
for (int i = 0; i < b.length(); i++) {
if (Integer.parseInt(b.substring(i, i+1)) != 0 || i+1 == b.length()) {
b = b.substring(i);
break;
}
}
//compare lengths of the two integers
if (a.length() < b.length()) {
System.out.println('<');
} else if (a.length() > b.length()) {
System.out.println('>');
//if they are the same length, iterate through each digit and find first point of difference
} else if (a.length() == b.length()) {
for (int i = 0; i < a.length(); i++) {
if (a.charAt(i) > b.charAt(i)) {
System.out.println('>');
return;
}
if (a.charAt(i) < b.charAt(i)) {
System.out.println('<');
return;
}
}
//if all tests pass (all digits are equal), return they are equal.
System.out.println('=');
return;
}
}
}