Java code behaving Weird!


#1

Has anyone faced the same problem before in Java? I am changing long from primitive to non-primitive. To be sure, I have done it multiple times.

public class Solution {
public int solve(ArrayList<Integer> A) {
    int n = A.size();
    long[] left = new long[n];
    long[] right = new long[n];
    /*
    This is giving me error
    Long[] left = new Long[n];
    Long[] right = new Long[n];
    */
    
    Long sum = 0L;
    for(int i = 0; i< n; i++){
        if((i % 2) == 0){
            sum += A.get(i);
        }
        else{
            sum -= A.get(i);
        }
        left[i] = sum;
    }
    sum = 0L;
    for(int i = n-1; i >=0; i--){
        if((i % 2) == 0){
            sum += A.get(i);
        }
        else{
            sum -= A.get(i);
        }
        right[i] = sum;
    }
    int ans = 0;
    for(int i = 0; i < n; i++){
        if(left[i] == right[i])
            ans++;
    }
    return ans;
}

}


#2
left[i] == right[i]

The culprit is the above line. I should use:

left[i].equals(right[i])

for non primitive.