Skip to content

Created FourSumProblem #5909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/java/com/thealgorithms/misc/FourSumProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.thealgorithms.misc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FourSumProblem {
public List<List<Integer>> fourSum(int[] nums, int target) {
int n = nums.length;
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);

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 k = j + 1;
int l = n - 1;

while (k < l) {
long sum = (long) nums[i] + nums[j] + nums[k] + nums[l];

if (sum == target) {
ans.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
k++;
l--;

while (k < l && nums[k] == nums[k - 1]) {
k++;
}
while (k < l && nums[l] == nums[l + 1]) {
l--;
}
} else if (sum < target) {
k++;
} else {
l--;
}
}
}
}
return ans;
}
}
78 changes: 78 additions & 0 deletions src/test/java/com/thealgorithms/misc/FourSumProblemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.thealgorithms.misc;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.List;

public class FourSumProblemTest {

private final FourSumProblem fourSumProblem = new FourSumProblem();

@Test
public void testFourSum_WithValidInput() {
int[] nums = {1, 0, -1, 0, -2, 2};
int target = 0;
List<List<Integer>> expected = Arrays.asList(
Arrays.asList(-2, 0, 0, 2),
Arrays.asList(-1, 0, 0, 1)
);

List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
assertEquals(expected.size(), result.size());
assertTrue(result.containsAll(expected));
}

@Test
public void testFourSum_WithNoSolution() {
int[] nums = {1, 2, 3, 4};
int target = 100;
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
assertTrue(result.isEmpty());
}

@Test
public void testFourSum_WithDuplicates() {
int[] nums = {1, 1, 1, 1};
int target = 4;
List<List<Integer>> expected = Arrays.asList(
Arrays.asList(1, 1, 1, 1)
);

List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
assertEquals(expected.size(), result.size());
assertTrue(result.containsAll(expected));
}

@Test
public void testFourSum_WithNegativeNumbers() {
int[] nums = {-3, -2, -1, 0, 0, 1, 2, 3};
int target = 0;
List<List<Integer>> expected = Arrays.asList(
Arrays.asList(-3, 1, 1, 1),
Arrays.asList(-2, 0, 0, 2),
Arrays.asList(-1, 0, 0, 1)
);

List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
assertEquals(expected.size(), result.size());
assertTrue(result.containsAll(expected));
}

@Test
public void testFourSum_WithEmptyArray() {
int[] nums = {};
int target = 0;
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
assertTrue(result.isEmpty());
}

@Test
public void testFourSum_WithLessThanFourElements() {
int[] nums = {1, 2, 3};
int target = 6;
List<List<Integer>> result = fourSumProblem.fourSum(nums, target);
assertTrue(result.isEmpty());
}
}
Loading