📖 Scenario: You are working on a shopping app that suggests combinations of four products whose prices add up to a customer's gift card amount. You want to find all unique sets of four product prices that sum exactly to the gift card value.
🎯 Goal: Build a C++ program that finds all unique quadruplets in a list of integers that sum to a target value.
📋 What You'll Learn
Create a vector of integers called nums with the exact values: {1, 0, -1, 0, -2, 2}
Create an integer variable called target and set it to 0
Implement a function called fourSum that takes nums and target and returns a vector of vectors of integers containing all unique quadruplets that sum to target
Print the resulting quadruplets in the format: [a, b, c, d] one per line
💡 Why This Matters
🌍 Real World
Finding combinations of products or items that meet 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 vector nums
Create a vector of integers called nums with these exact values: {1, 0, -1, 0, -2, 2}
DSA C++
Hint
Use std::vector<int> and initialize it with the given values inside curly braces.
2
Create the target integer variable target
Create an integer variable called target and set it to 0
DSA C++
Hint
Declare int target = 0; inside main().
3
Implement the fourSum function
Implement a function called fourSum that takes std::vector<int>& nums and int target and returns a std::vector<std::vector<int>> containing all unique quadruplets that sum to target. Use sorting and two-pointer technique inside nested loops.
DSA C++
Hint
Sort the vector 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 all unique quadruplets
Call the fourSum function with nums and target. Then print each quadruplet in the format [a, b, c, d] on its own line.
DSA C++
Hint
Call fourSum(nums, target) and store the result. Then use a for loop to print each quadruplet in the format [a, b, c, d].
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 with a given sum
Solution
Step 1: Understand the problem statement
The Four Sum Problem asks to find all unique sets of four numbers in an array that add up to a specific target sum.
Step 2: Identify the goal
The goal is to find quadruplets (groups of four numbers) that sum exactly to the target, without duplicates.
Final Answer:
Find all unique quadruplets in an array that sum up to a target value -> Option A
Quick Check:
Four Sum Problem = Find quadruplets summing to target [OK]
Hint: Look for four numbers adding to target without repeats [OK]
Common Mistakes:
Confusing with pair or triplet sum problems
Ignoring uniqueness of quadruplets
Trying to sort only without finding quadruplets
2. Which of the following C++ code snippets correctly initializes a vector to store quadruplets for the Four Sum Problem?
easy
A. std::vector<int> quadruplets(4);
B. std::vector quadruplets;
C. std::vector<int> quadruplets = {4};
D. std::vector> quadruplets;
Solution
Step 1: Understand the data structure needed
We need to store multiple quadruplets, each is a vector of 4 integers, so a vector of vector<int> is suitable.
Step 2: Check each option
std::vector quadruplets; declares a vector of vector<int>, perfect for storing multiple quadruplets. std::vector<int> quadruplets(4); creates a single vector of size 4, not multiple quadruplets. std::vector<int> quadruplets = {4}; initializes a vector with one element 4, incorrect. std::vector> quadruplets; uses pairs, which store only two integers, not four.
Final Answer:
std::vector quadruplets; -> Option B
Quick Check:
Use vector of vector<int> for quadruplets [OK]
Hint: Use vector of vector<int> to store multiple quadruplets [OK]
Common Mistakes:
Using vector<int> for multiple quadruplets
Using pairs instead of quadruplets
Initializing vector with wrong size or values
3. What will be the output of the following C++ code snippet for input array {1, 0, -1, 0, -2, 2} and target 0?
std::vector<int> nums = {1, 0, -1, 0, -2, 2};
int target = 0;
// Assume fourSum function returns all unique quadruplets
auto result = fourSum(nums, target);
for (auto &quad : result) {
for (int num : quad) std::cout << num << " ";
std::cout << std::endl;
}
medium
A. [-2 -1 1 2]
[-2 0 0 2]
[-1 0 0 1]
B. [-2 -1 0 1]
[-1 0 0 2]
C. [-2 -2 1 3]
[-1 0 0 1]
D. No quadruplets found
Solution
Step 1: Sort the array and find quadruplets summing to target 0
Sorted array: [-2, -1, 0, 0, 1, 2]. The unique quadruplets summing to 0 are [-2, -1, 1, 2], [-2, 0, 0, 2], and [-1, 0, 0, 1].
Step 2: Verify output matches quadruplets
[-2 -1 1 2]
[-2 0 0 2]
[-1 0 0 1] lists exactly these three quadruplets. Options A, C, and D have incorrect quadruplets or sums.