📖 Scenario: You are working on a financial app that analyzes daily stock price changes. You want to find all unique sets of three days where the total price change sums to zero. This helps identify balanced periods where gains and losses cancel out.
🎯 Goal: Build a program that finds all unique triplets in an integer array whose sum is zero. The output should list each triplet once, sorted in ascending order within the triplet.
📋 What You'll Learn
Create a vector of integers named nums with the exact values: {-1, 0, 1, 2, -1, -4}.
Create an integer variable n to store the size of nums.
Implement the three sum logic using sorting and two pointers to find all unique triplets that sum to zero.
Store the results in a vector of vectors named result.
Print each triplet in result on its own line in the format: [a, b, c].
💡 Why This Matters
🌍 Real World
Finding balanced triplets in financial data helps identify periods where gains and losses offset each other, useful for risk analysis.
💼 Career
This problem is common in coding interviews and helps develop skills in sorting, two-pointer technique, and handling duplicates.
Progress0 / 4 steps
1
Create the input vector
Create a vector of integers called nums with these exact values: {-1, 0, 1, 2, -1, -4}.
DSA C++
Hint
Use std::vector<int> and initialize it with the given values inside curly braces.
2
Add size variable
Create an integer variable called n and set it to the size of nums using nums.size().
DSA C++
Hint
Use int n = nums.size(); to get the number of elements in the vector.
3
Implement three sum logic
Sort the vector nums. Create a vector of vectors of integers called result. Use a for loop with variable i from 0 to n - 3. Inside the loop, use two pointers left and right to find all unique triplets that sum to zero and add them to result.
DSA C++
Hint
Sort the vector first. Use a for loop for the first element. Use two pointers to find pairs that sum with nums[i] to zero. Skip duplicates to avoid repeated triplets.
4
Print all unique triplets
Use a for loop with variable triplet to iterate over result. Inside the loop, print each triplet in the format [a, b, c] where a, b, and c are the three integers in the triplet.
DSA C++
Hint
Use a range-based for loop to go through each triplet in result. Print the three numbers inside square brackets separated by commas.
Practice
(1/5)
1. What is the main goal of the Three Sum Problem All Unique Triplets in an integer array?
easy
A. Find all unique triplets that sum to zero
B. Find the maximum sum of any three numbers
C. Find the triplet with the smallest product
D. Find pairs that sum to zero
Solution
Step 1: Understand the problem statement
The problem asks to find all unique sets of three numbers in the array whose sum equals zero.
Step 2: Clarify uniqueness and sum condition
Each triplet must be unique (no duplicates) and the sum of the three numbers must be exactly zero.
Final Answer:
Find all unique triplets that sum to zero -> Option A
Quick Check:
Unique triplets with sum zero = Find all unique triplets that sum to zero [OK]
Hint: Look for three numbers adding exactly to zero [OK]
Common Mistakes:
Confusing pairs with triplets
Ignoring uniqueness of triplets
Looking for max or min sums instead of zero sum
2. Which of the following C++ code snippets correctly sorts the input vector nums before applying the two-pointer technique in the Three Sum problem?
easy
A. std::sort(nums);
B. sort(nums, nums + nums.size());
C. nums.sort();
D. std::sort(nums.begin(), nums.end());
Solution
Step 1: Identify correct sorting syntax for std::vector
For a vector, sorting requires iterators: nums.begin() and nums.end().
Step 2: Check each option
std::sort(nums.begin(), nums.end()); uses correct syntax. sort(nums, nums + nums.size()); is invalid because nums is a vector, not a pointer array. nums.sort(); is invalid because vector has no sort() method. std::sort(nums); is invalid because std::sort requires two iterators.