-
Notifications
You must be signed in to change notification settings - Fork 0
/
PeculiarBalance.java
77 lines (57 loc) · 1.27 KB
/
PeculiarBalance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package Google;
import java.util.ArrayList;
import java.util.List;
// FooBar - Convert decimal to balanced ternary
public class PeculiarBalance {
public static void main(String[] args) {
int x = 546;
String[] str = answer(x);
for (String s : str)
System.out.println(s);
}
private static String[] answer(int x) {
return balancedTernary(decimalToTernary(x));
}
// find convert ternary to balanced ternary representation.
private static String[] balancedTernary(List<Integer> ternary) {
List<String> arr = new ArrayList<String>();
int carry = 0;
int i =0;
for (i = 0 ; i < ternary.size(); i++) {
int temp = ternary.get(i) + carry;
carry = 0;
switch (temp) {
case 3:
arr.add(i, "-");
carry = 1;
break;
case 2:
arr.add(i, "L");
carry = 1;
break;
case 1:
arr.add(i, "R");
carry = 0;
break;
case 0:
arr.add(i, "-");
carry = 0;
break;
}
}
if (carry == 1)
arr.add(i, "R");
return arr.toArray(new String[arr.size()]);
}
// convert decimal to ternary representation
private static List<Integer> decimalToTernary(int x) {
List<Integer> list = new ArrayList<Integer>();
int i = 0;
while (x > 0) {
list.add(i, (int) (x % 3));
x = x / 3;
i++;
}
return list;
}
}