Practice
Solution
Step 1: Understand problem constraints
The problem requires counting triplets with sum less than a target, which suggests checking combinations efficiently.Step 2: Identify approach that reduces complexity
Sorting and using two pointers allows scanning pairs efficiently for each fixed element, reducing complexity from O(n^3) to O(n^2).Final Answer:
Option B -> Option BQuick Check:
Sorting + two pointers is a classic pattern for 3Sum variants [OK]
- Assuming brute force is optimal
- Thinking DP applies directly here
- Believing greedy always works for sum constraints
threeSumSmaller([-2, 0, 1, 3], 2)?Solution
Step 1: Sort input and initialize variables
Sorted array is [-2, 0, 1, 3], count=0, n=4.Step 2: Iterate and count triplets
For i=0 (-2), check if sum of first three elements >= 2? -2+0+1= -1 < 2, continue. Use two pointers left=1, right=3. - curr_sum = -2 + 0 + 3 = 1 < 2 -> count += 3-1=2, left=2 - curr_sum = -2 + 1 + 3 = 2 not < 2 -> right=2 Now left=2, right=2 ends loop. For i=1 (0), sum of nums[1]+nums[2]+nums[3] = 0+1+3=4 >= 2 -> break loop. Total count=2.Final Answer:
Option A -> Option AQuick Check:
Matches expected output from example [OK]
- Off-by-one in counting pairs
- Not breaking early when minimal sum >= target
- Misusing pointers leading to wrong counts
Solution
Step 1: Understand problem constraints
Finding quadruplets summing to target requires checking combinations efficiently and avoiding duplicates.Step 2: Evaluate approaches
Brute force (four loops) is correct but inefficient. Greedy fails because sum combinations are not monotonic. DP is not suitable here. Hash map storing pairs of sums allows O(n^2) preprocessing and efficient lookup, handling duplicates by sorting and index checks.Final Answer:
Option D -> Option DQuick Check:
Hash map approach balances efficiency and correctness [OK]
- Thinking greedy or DP solves 4Sum efficiently
nums = [1, 0, -1, 0] and target = 0, what is the final returned list of quadruplets?Solution
Step 1: Sort input and build two_sum_map
Sorted nums = [-1, 0, 0, 1]. Pairs and sums: (-1,0)= -1, (-1,0)= -1, (-1,1)=0, (0,0)=0, (0,1)=1, (0,1)=1.Step 2: Find complementary pairs summing to target=0
Check sums and their complements: sum1=-1, sum2=1; sum1=0, sum2=0. Combine pairs ensuring no index overlap. Valid quadruplet is [-1,0,0,1].Final Answer:
Option A -> Option AQuick Check:
Only one quadruplet sums to zero without index overlap [OK]
- Confusing indices leading to duplicates or missing quadruplets
from typing import List
from collections import defaultdict
def fourSum(nums: List[int], target: int) -> List[List[int]]:
nums.sort()
n = len(nums)
two_sum_map = defaultdict(list)
res = set()
for i in range(n):
for j in range(i + 1, n):
two_sum_map[nums[i] + nums[j]].append((i, j))
for sum1 in two_sum_map:
sum2 = target - sum1
if sum2 not in two_sum_map:
continue
list1 = two_sum_map[sum1]
list2 = two_sum_map[sum2]
for (i, j) in list1:
for (k, l) in list2:
# BUG: Missing index overlap check
quad = tuple(sorted([nums[i], nums[j], nums[k], nums[l]]))
res.add(quad)
return [list(quad) for quad in sorted(res)]Solution
Step 1: Identify where index overlap is checked
The code must ensure indices i,j,k,l are distinct to avoid repeated elements.Step 2: Locate missing check
The nested loops combining pairs lack any condition verifying indices are unique, allowing invalid quadruplets.Final Answer:
Option A -> Option AQuick Check:
Missing index overlap check causes subtle bug [OK]
- Forgetting index uniqueness check in pair combinations
