maximum subarray sum equal with K in c++

C++
int maxsubarraysumequaltoX(int A[], int N, int X) {
    int i = 0, j = 0, best = A[0], res = 0;
    while (j < N) {
        if (best == X) res = max(res, j-i+1);
        if (best <= X) {
            if (j < N-1) best += A[++j];
            else j++;
        }
        else best -= A[i++];
    }
    return res;
}

Source

Also in C++: