summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java59
-rw-r--r--AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java72
2 files changed, 131 insertions, 0 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java b/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java
new file mode 100644
index 0000000..0458b91
--- /dev/null
+++ b/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java
@@ -0,0 +1,59 @@
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Scanner;
+
+public class LargestNumber {
+
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+ int n = scanner.nextInt();
+ String[] a = new String[n];
+ for (int i = 0; i < n; i++) {
+ a[i] = scanner.next();
+ }
+ System.out.println(getLargestNumber(a));
+ }
+
+ public static String getLargestNumber(String[] numbers) {
+ List<String> numList = new ArrayList<String>(Arrays.asList(numbers));
+ String result = "";
+ String maxDigit = "0";
+ int indexHolder = 0;
+ while (numList.size() > 0) {
+ for (int i = 0; i < numList.size(); i++) {
+ if (compareLeadingDigit(numList.get(i), maxDigit)) {
+ maxDigit = numList.get(i);
+ indexHolder = i;
+ }
+ }
+ result = result + maxDigit;
+ maxDigit = "0";
+ numList.remove(indexHolder);
+ }
+ return result;
+ }
+
+ public static boolean compareLeadingDigit(String string, String maxDigit) {
+ if (string.length() == maxDigit.length())
+ return Integer.parseInt(string) > Integer.parseInt(maxDigit);
+ if (string.length() < maxDigit.length()) {
+ if (string.charAt(0) > maxDigit.charAt(0)) {
+ return true;
+ } else if (string.charAt(0) < maxDigit.charAt(0)) {
+ return false;
+ } else if (string.charAt(0) > maxDigit.charAt(1)) {
+ return true;
+ } else if (string.charAt(0) < maxDigit.charAt(1)) {
+ return false;
+ } else if (maxDigit.length() == 2)
+ return false;
+ else {
+ return string.charAt(0) > maxDigit.charAt(2);
+ }
+ } else {
+ return !compareLeadingDigit(maxDigit, string);
+ }
+ }
+
+}
diff --git a/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java b/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java
new file mode 100644
index 0000000..285d502
--- /dev/null
+++ b/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java
@@ -0,0 +1,72 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.Test;
+
+class LargestNumberTest {
+
+ @Test
+ void test1() {
+ String[] numbers = {"9", "4", "6", "1", "9"};
+ assertEquals("99641", LargestNumber.getLargestNumber(numbers));
+ }
+
+ @Test
+ void test2() {
+ String[] numbers = {"21", "2"};
+ assertEquals("221", LargestNumber.getLargestNumber(numbers));
+ }
+
+ @Test
+ void test3() {
+ String[] numbers = {"23", "39", "92"};
+ assertEquals("923923", LargestNumber.getLargestNumber(numbers));
+ }
+
+ @Test
+ void test4() {
+ String[] numbers = {"23", "39", "92"};
+ assertEquals("923923", LargestNumber.getLargestNumber(numbers));
+ }
+
+ @Test
+ void test5() {
+ String[] numbers = {"23", "2"};
+ assertEquals("232", LargestNumber.getLargestNumber(numbers));
+ }
+
+ @Test
+ void testCompareLeadingDigit() {
+ assertTrue(LargestNumber.compareLeadingDigit("2", "21"));
+ }
+
+ @Test
+ void testCompareLeadingDigit1() {
+ assertFalse(LargestNumber.compareLeadingDigit("20", "21"));
+ }
+
+ @Test
+ void testCompareLeadingDigit2() {
+ assertFalse(LargestNumber.compareLeadingDigit("321", "322"));
+ }
+
+ @Test
+ void testCompareLeadingDigit3() {
+ assertFalse(LargestNumber.compareLeadingDigit("321", "32"));
+ }
+
+ @Test
+ void testCompareLeadingDigit4() {
+ assertFalse(LargestNumber.compareLeadingDigit("10", "1"));
+ }
+
+ @Test
+ void testCompareLeadingDigit5() {
+ assertTrue(LargestNumber.compareLeadingDigit("6", "10"));
+ }
+
+ @Test
+ void testCompareLeadingDigit6() {
+ assertTrue(LargestNumber.compareLeadingDigit("23", "2"));
+ }
+
+}