Computer scienceProgramming languagesJavaInterview preparationAlgorithms and problem solving techniques

Understand the problem

4Sum problem

Report a typo

You are given an array nums of n integers. Return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

  • 0 <= a, b, c, d < n
  • a, b, c, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target
  • You may return the answer in any order.

The first line of the test case input is the array, and the second line of the input is the target.

Sample Input 1:

1 0 -1 0 -2 2
0

Sample Output 1:

[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
Write a program in Java 17
import java.util.Scanner;

class FourSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] nums = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int target = Integer.parseInt(scanner.nextLine());

//write your code here

}
}
___

Create a free account to access the full topic