Skip to content

Bug Report for 4sum #4449

@petienne1

Description

@petienne1

Bug Report for https://neetcode.io/problems/4sum

The test environment is expecting a function called threeSum, but this is fourSum.

This is the solution I am attempting to pass and it keeps failing.

function fourSum(nums, target) {
if (nums.length < 4) return [];
nums.sort((a, b) => a - b);

for (let i = 0; i < nums.length - 3; i++) {
    for (let j = i + 1; j < nums.length - 2; j++) {
        let left = j + 1;
        let right = nums.length - 1;

        while (left < right) {
            let sum = nums[i] + nums[j] + nums[left] + nums[right];

            if (sum === target) {
                return [[nums[i], nums[j], nums[left], nums[right]]];
            } else if (sum < target) {
                left++;
            } else {
                right--;
            }
        }
    }
}

return [];

}

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