📖 Scenario: You are working on a shopping app that suggests combinations of four products whose prices add up to a customer's gift card value. You want to find all unique sets of four product prices that exactly match the gift card amount.
🎯 Goal: Build a Java program that finds all unique quadruplets in an integer array that sum up to a given target value.
📋 What You'll Learn
Create an integer array called nums with the exact values: 1, 0, -1, 0, -2, 2
Create an integer variable called target and set it to 0
Write a method called fourSum that takes nums and target as parameters and returns a list of lists of integers representing all unique quadruplets that sum to target
Print the list of quadruplets returned by fourSum
💡 Why This Matters
🌍 Real World
Finding combinations of products or items that match a specific budget or target sum is common in shopping apps, budgeting tools, and financial planning software.
💼 Career
Understanding how to find unique combinations in arrays is useful for software engineers working on recommendation systems, data analysis, and algorithm optimization.
Progress0 / 4 steps
1
Create the input array nums
Create an integer array called nums with these exact values: 1, 0, -1, 0, -2, 2
DSA Java
Hint
Use int[] nums = {1, 0, -1, 0, -2, 2}; to create the array.
2
Create the target sum variable target
Create an integer variable called target and set it to 0
DSA Java
Hint
Use int target = 0; to create the target variable.
3
Write the fourSum method to find unique quadruplets
Write a method called fourSum that takes int[] nums and int target as parameters and returns a List> of all unique quadruplets that sum to target. Use sorting and two pointers inside nested loops to find quadruplets. The method signature must be public static List> fourSum(int[] nums, int target).
DSA Java
Hint
Sort the array first. Use two nested loops for the first two numbers, then use two pointers to find the other two numbers. Skip duplicates to ensure unique quadruplets.
4
Print the result of fourSum(nums, target)
Call the fourSum method with nums and target and print the returned list of quadruplets.
DSA Java
Hint
Call fourSum(nums, target) and print the result using System.out.println.
Practice
(1/5)
1. What is the main goal of the Four Sum Problem in data structures and algorithms?
easy
A. Find all unique quadruplets in an array that sum up to a target value
B. Sort the array in ascending order
C. Find the maximum element in the array
D. Count the number of pairs that sum to a target
Solution
Step 1: Understand the problem statement
The Four Sum Problem asks to find sets of four numbers in an array that add up to a given target.
Step 2: Identify the expected output
We want all unique quadruplets, meaning no repeated sets, that sum exactly to the target.
Final Answer:
Find all unique quadruplets in an array that sum up to a target value -> Option A
Quick Check:
Four Sum = unique quadruplets summing to target [OK]
Hint: Look for four numbers adding to target without duplicates [OK]
Common Mistakes:
Confusing Four Sum with Two Sum or Three Sum problems
Ignoring uniqueness of quadruplets
Trying to find maximum or minimum instead of sum
2. Which of the following Java code snippets correctly initializes an empty list to store quadruplets for the Four Sum problem?
easy
A. List quadruplets = new List<>();
B. List quadruplets = new ArrayList<>();
C. ArrayList<int> quadruplets = new ArrayList<>();
D. List<List> quadruplets = new ArrayList();
Solution
Step 1: Check Java syntax for list initialization
Correct generic syntax for a list of lists of integers is List quadruplets = new ArrayList<>();
Step 2: Identify errors in other options
List quadruplets = new List<>(); uses raw List without generics and wrong constructor; ArrayList<int> quadruplets = new ArrayList<>(); uses primitive int which is invalid; List<List> quadruplets = new ArrayList(); mixes generics incorrectly.
Final Answer:
List quadruplets = new ArrayList<>(); -> Option B
Quick Check:
Correct generic list initialization = List quadruplets = new ArrayList<>(); [OK]
Hint: Use List with ArrayList constructor [OK]
Common Mistakes:
Using raw types without generics
Trying to use primitive types in generics
Incorrect constructor calls for List
3. What will be the output of the following Java code snippet for the Four Sum problem?
int[] nums = {1, 0, -1, 0, -2, 2};
int target = 0;
List result = fourSum(nums, target);
System.out.println(result);
Assuming fourSum correctly finds all unique quadruplets summing to target.
medium
A. [[-2, -1, 0, 1], [0, 0, 0, 0]]
B. [[1, 0, -1, 0]]
C. [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
D. []
Solution
Step 1: Sort and find quadruplets summing to zero
Sorted array: [-2, -1, 0, 0, 1, 2]. Quadruplets summing to 0 are [-2,-1,1,2], [-2,0,0,2], and [-1,0,0,1].
Step 2: Verify uniqueness and completeness
All quadruplets are unique and sum exactly to target 0. No other quadruplets exist.