What's wrong in this code


#1
int Solution::diffPossible(vector<int> &A, int B) {
int i,n=A.size(),j,sum;
//map<int,int> m;
i=0,j=1;
while(j<n)
{
    sum=A[j]-A[i];
    if(sum==B)
    {
        return 1;
    }
    if(sum>B)
    {
        i++;
    }
    else
    {
        j--;
    }
    if(i==j)
    {
        j++;
    }
}
return 0;

}


#2

See for the else part, it must be j++; instead of j–;
Since when the sum is less then we need to increase it so j++;
Example: A[] = 0 1 9 10
B = 10
using j-- it will go into infinite loop because of the condition if(i==j) then j++;