Challenge - 5 Problems
Three Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Three Sum Unique Triplets Algorithm
What is the output of the following Java code that finds all unique triplets in an array which sum to zero?
DSA Java
import java.util.*; public class Solution { public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { res.add(Arrays.asList(nums[i], nums[left], nums[right])); while (left < right && nums[left] == nums[left + 1]) left++; while (left < right && nums[right] == nums[right - 1]) right--; left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return res; } public static void main(String[] args) { int[] nums = {-1, 0, 1, 2, -1, -4}; List<List<Integer>> result = threeSum(nums); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Remember the algorithm sorts the array and skips duplicates to find unique triplets summing to zero.
✗ Incorrect
The code sorts the array and uses two pointers to find pairs that sum with nums[i] to zero. It skips duplicates to avoid repeated triplets. The unique triplets are [-1, -1, 2] and [-1, 0, 1].
🧠 Conceptual
intermediateUnderstanding Duplicate Skipping in Three Sum
Why does the algorithm skip elements when nums[i] == nums[i - 1] during iteration?
Attempts:
2 left
💡 Hint
Think about how duplicates affect the uniqueness of triplets.
✗ Incorrect
Skipping when nums[i] == nums[i - 1] prevents generating duplicate triplets starting with the same number, ensuring all triplets in the result are unique.
🔧 Debug
advancedIdentify the Error in Modified Three Sum Code
What error will this modified code produce when run?
DSA Java
import java.util.*; public class Solution { public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { int left = i + 1, right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { res.add(Arrays.asList(nums[i], nums[left], nums[right])); left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return res; } public static void main(String[] args) { int[] nums = {-1, 0, 1, 2, -1, -4}; List<List<Integer>> result = threeSum(nums); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check what happens if duplicates are not skipped in the loop.
✗ Incorrect
The modified code does not skip duplicates for nums[i], left, or right pointers, so duplicate triplets appear in the output.
❓ Predict Output
advancedOutput of Three Sum with All Positive Numbers
What is the output of the threeSum method when the input array contains only positive numbers?
DSA Java
import java.util.*; public class Solution { public static List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) { if (i > 0 && nums[i] == nums[i - 1]) continue; int left = i + 1, right = nums.length - 1; while (left < right) { int sum = nums[i] + nums[left] + nums[right]; if (sum == 0) { res.add(Arrays.asList(nums[i], nums[left], nums[right])); while (left < right && nums[left] == nums[left + 1]) left++; while (left < right && nums[right] == nums[right - 1]) right--; left++; right--; } else if (sum < 0) { left++; } else { right--; } } } return res; } public static void main(String[] args) { int[] nums = {1, 2, 3, 4, 5}; List<List<Integer>> result = threeSum(nums); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Sum of three positive numbers cannot be zero.
✗ Incorrect
Since all numbers are positive, no three numbers can sum to zero, so the result is an empty list.
🚀 Application
expertNumber of Unique Triplets Summing to Zero
Given the input array [-2, 0, 1, 1, 2, -1, -1], how many unique triplets sum to zero?
Attempts:
2 left
💡 Hint
Find all unique triplets without duplicates that sum to zero.
✗ Incorrect
The unique triplets are [-2, 0, 2], [-2, 1, 1], [-1, -1, 2], and [-1, 0, 1], totaling 4.
