Skip to content

4Sums july challenge Line 16: Char 45: runtime error: signed integer overflow: -294967296 - 2000000000 cannot be represented in type 'int' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:25:45 still facing this issue #20

@AbhayPandey20

Description

@AbhayPandey20

class Solution {
public:
vector<vector> fourSum(vector& nums, int target) {
sort(nums.begin(), nums.end());
vector<vector> ans;
int n = nums.size();
for(int i = 0; i < n - 3; i++) {
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for(int j = i + 1; j < n - 2; j++) {
if(j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}

            long long int tSum = target - (static_cast<long long int>(nums[i]) + nums[j]);
            long long int l = j + 1, h = n - 1;
            while (l < h) {
                long long int sum = nums[l] + nums[h];
                if (sum == tSum) {
                    vector<int> oneAns = {nums[i], nums[j], nums[l], nums[h]};
                    ans.push_back(oneAns);
                    while(l < h && nums[l] == nums[l + 1]) {
                        l++;
                    }
                    while(l < h && nums[h] == nums[h - 1]) {
                        h--;
                    }
                    l++;
                    h--;
                } else if(sum < tSum) {
                    l++;
                } else {
                    h--;
                }
            }
        }
    }
      
    return ans;
}

};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions