• + 0 comments

    Only O(M) space is needed. The question was tagged "sparse arrays" but this is a brute-force, dense array.

    if((q+1)<=N) is unnecessary because [N+1] space is allocated.