Skip to content

Commit e1bcf70

Browse files
committed
Runtime: 1 ms (Top 100.00%) | Memory: 42.4 MB (Top 98.61%)
1 parent 2f532c6 commit e1bcf70

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1+
// Runtime: 1 ms (Top 100.00%) | Memory: 42.4 MB (Top 98.61%)
12
class Solution {
23
public int[] addNegabinary(int[] arr1, int[] arr2) {
3-
4+
45
List<Integer> result = new ArrayList();
56
int pointer_1 = arr1.length-1;
67
int pointer_2 = arr2.length-1;
78

89
int carry = 0;
910
int current = 0;
1011
int sum = 0;
11-
12+
1213
while(pointer_1 >= 0 || pointer_2 >= 0){
13-
14+
1415
int a = (pointer_1 >=0)? arr1[pointer_1]: 0;
1516
int b = (pointer_2 >=0)? arr2[pointer_2]: 0;
16-
17+
1718
sum = a+b+carry;
1819
if(sum == 3){
1920
current = 1; carry = -1;
@@ -31,31 +32,31 @@ else if(sum == -1)
3132
{
3233
current = 1; carry = 1;
3334
}
34-
35+
3536
result.add(current);
3637
pointer_1--;
3738
pointer_2--;
3839
}
39-
40+
4041
if(carry != 0)
4142
result.add(1);
4243
if(carry == -1)
4344
result.add(1);
44-
45+
4546
// Removing leading zeros
4647
int idx = result.size()-1;
4748
while(idx > 0 && result.get(idx) == 0)
4849
idx--;
49-
50+
5051
// reversing the list and adding the result to an array
5152
int len = idx+1;
5253
int[] negaBinary = new int[len];
5354
for(int i=0; i<len; i++){
5455
negaBinary[i] = result.get(idx);
5556
idx--;
5657
}
57-
58+
5859
return negaBinary;
59-
60+
6061
}
6162
}

0 commit comments

Comments
 (0)