Errors in 3sum and 4sum

I attempted Leet Code 3 sum and 4sum problems, but couldn’t understand the errors. Please help.

I think both the codes are same, but strange memory errors are displayed.

The code which displays error in the server runs well in my machine.

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& N) {
        
        sort(N.begin(), N.end());
        int n = N.size();
        vector<vector<int>> A;
        
        for(int i = 0; i < n-2; i++)
        {
            if(i > 0 and N[i] == N[i-1])
                continue;
            int l = i+1, r = n-1;
            while(l < r)
            {
                if(N[i] + N[l] + N[r] > 0)
                    r--;
                else if(N[i] + N[l] + N[r] < 0)
                    l++;
                else
                {
                    A.push_back({N[i], N[l], N[r]});
                    int x = N[l];
                    int y = N[r];
                    while(x == N[l] and l < n)
                        l++;
                    while(y == N[r] and r >= 0)
                        r--;
                    //l++, r--;
                }
            }
        }
        
        return A;
    }
};
Verdict :- Line 1034: Char 34: runtime error: addition of unsigned offset to 0x6020000005b0 overflowed to 0x6020000005ac (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34
class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        
    int n = nums.size();
	vector<vector<int>> ans;
	sort(nums.begin(), nums.end());

	for(int i = 0; i < n-2; i++) {
		int j = i + 1, k = n-1;
		while(j < k) {
			int sum = nums[i] + nums[j] + nums[k];
			if(sum > 0) k--;
			else if(sum < 0) j++;
			else {
				ans.push_back({nums[i], nums[j], nums[k]});
				while(j < n-1 && nums[j] == nums[j+1]) j++;
				while(k > 0 && nums[k] == nums[k-1]) k--;
				j++, k--;
			}
		}

		while(i < n-1 && nums[i] == nums[i+1]) i++;
	}

	return ans;
    }
};

Verdict :- Accepted

And now for the 4 sum problem,

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& N, int target) {
        
        int n = N.size();
        vector<vector<int>> A;
        sort(N.begin(), N.end());
        for(int i = 0; i < n-3; i++)
        {
            if(i != 0 and N[i] == N[i-1])
                continue;
            for(int j = i+1; j < n-2; j++)
            {
                if(j != (i+1) and N[j] == N[j-1])
                    continue;
                int l = j+1, r = n-1;
                while(l < r)
                {
                    int sum = N[i] + N[j] + N[l] + N[r];
                    if(sum > target)
                        r--;
                    else if(sum < target)
                        l++;
                    else
                    {
                        A.push_back({N[i], N[j], N[l], N[r]});
                        while(N[l] == N[l+1] and l < n-1)
                            l++;
                        while(N[r] == N[r-1] and r > 0)
                            r--;
                        l++, r--;
                    }
                }
            }
        }
        return A;
    }
};
Verdict :- Line 1034: Char 34: runtime error: addition of unsigned offset to 0x6030000000d0 overflowed to 0x6030000000cc (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_vector.h:1043:34

And for another code :-

vector<vector<int>> fourSum(vector<int>& nums, int target) {
        int n=nums.size();
        vector<vector<int>> ans;
        sort(nums.begin(),nums.end());
        for(int i=0;i<n;i++){
            if(i>0 && nums[i]==nums[i-1])
                continue;
            for(int j=i+1;j<n;j++){
                if(j!=(i+1) && nums[j]==nums[j-1])
                    continue;
                int rem=target-(nums[i]+nums[j]);
                int p1=j+1;
                int p2=n-1;
                while(p1<p2){
                    int curr=nums[p1]+nums[p2];
                    if(curr<rem)
                        p1++;
                    else if(curr>rem)
                        p2--;
                    else{
                        ans.push_back({nums[i],nums[j],nums[p1],nums[p2]});
                        if(nums[p1]==nums[p2])
                            break;
                            int x=nums[p1];
                            int y=nums[p2];
                            while(x==nums[p1])
                                p1++;
                            while(y==nums[p2])
                                p2--;
                        
                    }
                }
            }
        }
        return ans;
    }

Status :- Accepted

Just for checking, I ran the following code in my machine :-

#include<bits/stdc++.h>
using namespace std;

vector<vector<int>> fourSum(vector<int>& N, int target) {
        
        int n = N.size();
        vector<vector<int>> A;
        sort(N.begin(), N.end());
        for(int i = 0; i < n-3; i++)
        {
            if(i != 0 and N[i] == N[i-1])
                continue;
            for(int j = i+1; j < n-2; j++)
            {
                if(j != (i+1) and N[j] == N[j-1])
                    continue;
                int l = j+1, r = n-1;
                while(l < r)
                {
                    int sum = N[i] + N[j] + N[l] + N[r];
                    if(sum > target)
                        r--;
                    else if(sum < target)
                        l++;
                    else
                    {
                        A.push_back({N[i], N[j], N[l], N[r]});
                        while(N[l] == N[l+1] and l < n-1)
                            l++;
                        while(N[r] == N[r-1] and r > 0)
                            r--;
                        l++, r--;
                    }
                }
            }
        }
        return A;
}

int main()
{
	int n, t;
	cin >> n >> t;
	vector<int> B(n);
	for(auto &x : B)
		cin >> x;
	vector<vector<int>>A = fourSum(B, t);
	for(int i = 0; i < A.size(); i++)
		cout << A[i][0] << " " << A[i][1] << " " << A[i][2] << " " << A[i][3] << "\n"; 
}

it worked fine. Please look into this issue.

Your solution for 3sum passed 1,000 randomly generated tests in comparison to my accepted code.

Hacking