[Programmers / JAVA] Level 2 Numbers of Country 124 (12899)
[Programmers / JAVA] Level 2 Numbers of Country 124 (12899)
| Rank | Language Used |
|---|---|
| Level 2 | 🖼️ JAVA |
There is a country called 124. In country 124, numbers are not represented in base 10, but instead follow their own unique rule as described below.
- In country 124, only natural numbers exist.
- In country 124, only the digits 1, 2, and 4 are used to represent all numbers.
For example, the numbers used in country 124 are converted as follows.
| Decimal | Country 124 | Decimal | Country 124 |
|---|---|---|---|
| 1 | 1 | 6 | 14 |
| 2 | 2 | 7 | 21 |
| 3 | 4 | 8 | 22 |
| 4 | 11 | 9 | 24 |
| 5 | 12 | 10 | 41 |
Given a natural number n as a parameter, complete the solution function to return n converted into the number system used in country 124.
- n is a natural number less than or equal to 500,000,000.
| n | result |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 4 |
| 4 | 11 |
There's a strange country called 124.
Instead of using the internationally common base-10 system, it insists on this odd 124-based rule, which can be quite tiring to deal with.
Let's build an algorithm to decode this base-124 system and finally get some rest.
The rule itself isn't complicated. Numbers are represented by alternating through [ 1, 2, 4 ].
| Decimal | Country 124 |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 4 |
| 4 | 11 |
| 5 | 12 |
| 6 | 14 |
| 7 | 21 |
| 8 | 22 |
| 9 | 24 |
| 10 | 41 |
| 11 | 42 |
| 12 | 44 |
| 13 | 111 |
| 14 | 112 |
| 15 | 114 |
That's roughly how it unfolds. It can be a bit confusing since the last digit is 4 instead of 3.
Since the data follows a regular pattern, let's use it to infer the underlying rule.
Because it's represented with 3 digits, it should work in a way very similar to constructing base 3.
We can convert to base 124 using the MOD operation.
- Perform n % 3 to get the remainder.
- Perform n / 3 and assign it to n.
- If n % 3 == 0, subtract 1 from the assigned n.
- Repeat the process above until n = 0.
For n = 5, this looks like:
- Perform 5 % 3 to get a remainder of 2.
- Since the second digit of 124 is 2, the first place becomes 2.
- Perform 5 / 3 to assign 1 to n.
- Perform 1 % 3 to get a remainder of 1.
- Since the first digit of 124 is 1, the second place becomes 1.
- Perform 1 / 3 to assign 0 to n.
- Since n = 0, terminate. 5 becomes 12 in base 124.
However, when n % 3 == 0, there's a special condition, which becomes clear if you work through a multiple of 3 directly.
- Perform 6 % 3 to get a remainder of 0.
- Since the 0th digit of 124 is 4, the first place becomes 4.
- Perform 6 / 3 to assign 2 to n.
- Perform 2 % 3 to get a remainder of 2.
- Since the second digit of 124 is 2, the second place becomes 2.
- Perform 2 / 3 to assign 0 to n.
- Since n = 0, terminate. 6 becomes 24 in base 124...?
According to this calculation it should be 24, but the actual answer is 14. For multiples of 3, the digit count increases by one, so we need to subtract 1 from n to correct for it.
JAVA
import java.util.ArrayList; import java.util.Collections; /** * Numbers of Country 124 class * * @author RWB * @since 2021.12.27 Mon 00:36:19 */ class Solution { private static final String[] DIGIT = { "4", "1", "2" }; /** * Answer return method * * @param n: [int] Natural number * * @return [String] Answer */ public String solution(int n) { ArrayList<String> list = new ArrayList<>(); while (n != 0) { int index = n % 3; n /= 3; // If the index is 0 if (index == 0) { n--; } list.add(DIGIT[index]); } Collections.reverse(list); return list.stream().reduce(String::concat).isPresent() ? list.stream().reduce(String::concat).get() : ""; } }
