-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathAdding Two Negabinary Numbers.java
62 lines (51 loc) · 1.53 KB
/
Adding Two Negabinary Numbers.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
// Runtime: 1 ms (Top 100.00%) | Memory: 42.4 MB (Top 98.61%)
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
List<Integer> result = new ArrayList();
int pointer_1 = arr1.length-1;
int pointer_2 = arr2.length-1;
int carry = 0;
int current = 0;
int sum = 0;
while(pointer_1 >= 0 || pointer_2 >= 0){
int a = (pointer_1 >=0)? arr1[pointer_1]: 0;
int b = (pointer_2 >=0)? arr2[pointer_2]: 0;
sum = a+b+carry;
if(sum == 3){
current = 1; carry = -1;
}
else if(sum == 2){
current = 0; carry = -1;
}
else if(sum == 1){
current = 1; carry = 0;
}
else if(sum == 0){
current = 0; carry = 0;
}
else if(sum == -1)
{
current = 1; carry = 1;
}
result.add(current);
pointer_1--;
pointer_2--;
}
if(carry != 0)
result.add(1);
if(carry == -1)
result.add(1);
// Removing leading zeros
int idx = result.size()-1;
while(idx > 0 && result.get(idx) == 0)
idx--;
// reversing the list and adding the result to an array
int len = idx+1;
int[] negaBinary = new int[len];
for(int i=0; i<len; i++){
negaBinary[i] = result.get(idx);
idx--;
}
return negaBinary;
}
}