[Programmers / JAVA] Level 1 Reverse Base 3 (68935)
[Programmers / JAVA] Level 1 Reverse Base 3 (68935)
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
A natural number n is given as a parameter. Complete the solution function so that it reverses n in base 3, then returns the result expressed again in base 10.
- n is a natural number between 1 and 100,000,000.
| n | result |
|---|---|
| 45 | 7 |
| 125 | 229 |
Input/Output Example #1
The process for deriving the answer is as follows.
| n (base 10) | n (base 3) | Reversed (base 3) | Expressed in base 10 |
|---|---|---|---|
| 45 | 1200 | 0021 | 7 |
So it must return 7.
Input/Output Example #2
The process for deriving the answer is as follows.
| n (base 10) | n (base 3) | Reversed (base 3) | Expressed in base 10 |
|---|---|---|---|
| 125 | 11122 | 22111 | 229 |
So it must return 229.
This algorithm converts an arbitrary number to base 3, arranges it in reverse order, and converts it back to base 10.
- Base 10 -> base 3 conversion algorithm
- Base 3 -> base 10 conversion algorithm
It seems best to build conversion modules so that base 10 and base 3 can be freely converted between each other.
Also, since the digits need to be arranged in reverse order in base 3, I plan to assign each digit individually into a List and reverse it all at once.
The method to convert from base 10 to base 3 is as follows.
JAVA
private ArrayList<Integer> dec2ter(int num) { ArrayList<Integer> list = new ArrayList<>(); int max = 0; while (Math.pow(3, max) <= num) { max++; } for (int i = max - 1; i > -1; i--) { int pow = (int) Math.pow(3, i); list.add(num / pow); num %= pow; } return list; }
Representing base 3 as an integer doesn't carry much meaning. If a 0 comes at the front, it's very likely to be lost once converted to an integer type, and there's no real need for arithmetic on the integer form anyway. So it's returned as ArrayList<Integer>.
JAVA
private int ter2dec(ArrayList<Integer> nums) { int flag = nums.size() - 1; int answer = 0; for (int num : nums) { answer += (int) Math.pow(3, flag) * num; flag--; } return answer; }
The base 3 -> base 10 conversion algorithm is as shown above.
Get the base 3 array with dec2ter, arrange it in reverse order, and then convert it with the ter2dec method. The Collections.reverse() method can be used to reverse a Collection object like a List.
JAVA
import java.util.ArrayList; import java.util.Collections; /** * Reverse Base 3 class * * @author RWB * @since 2021.12.12 Sun 00:47:33 */ class Solution { /** * Method that returns the answer * * @param n: [int] natural number * * @return [int] the answer */ public int solution(int n) { ArrayList<Integer> list = dec2ter(n); Collections.reverse(list); return ter2dec(list); } /** * Method that converts base 10 -> base 3 array * * @param num: [int] natural number * * @return [ArrayList] base 3 array */ private ArrayList<Integer> dec2ter(int num) { ArrayList<Integer> list = new ArrayList<>(); int max = 0; while (Math.pow(3, max) <= num) { max++; } for (int i = max - 1; i > -1; i--) { int pow = (int) Math.pow(3, i); list.add(num / pow); num %= pow; } return list; } /** * Method that converts base 3 -> base 10 * * @param nums: [ArrayList] base 3 array * * @return [int] base 10 number */ private int ter2dec(ArrayList<Integer> nums) { int flag = nums.size() - 1; int answer = 0; for (int num : nums) { answer += (int) Math.pow(3, flag) * num; flag--; } return answer; } }
