The two's complement of an N-bit number is defined as the complement with respect to 2^N; in other words, it is the result of subtracting the number from 2^N
public class NumArray {
int[] fenwickTree;
int length;
int[] arr;
public NumArray(int[] nums) {
length = nums.length;
arr = new int[length];
fenwickTree = new int[length + 1];
for(int i = 0; i < length; i++){
update(i, nums[i]);
}
}
void update(int i, int val) {
int diff = val - arr[i];
arr[i] = val;
for(int index = i + 1; index <= length; index += (index & -index)){
fenwickTree[index] += diff;
}
}
private int getSum(int i){
int sum = 0;
while(i > 0){
sum += fenwickTree[i];
i -= (i & -i);
}
return sum;
}
public int sumRange(int i, int j) {
return getSum(j + 1) - getSum(i);
}
}