3Sum With Multiplicity - Problem

Given an integer array arr and a target sum target, find all unique triplets of indices (i, j, k) where i < j < k and arr[i] + arr[j] + arr[k] == target.

The twist? We need to count all possible combinations, including those with duplicate values. Since the result can be extremely large, return the answer modulo 109 + 7.

Example: For array [1,1,2,2,3,3,4,4,5,5] with target = 8, we need to find all ways to pick three numbers (at different positions) that sum to 8. This includes combinations like (1,2,5), (1,3,4), and (2,2,4), considering all possible index arrangements.

This problem combines the classic 3Sum challenge with combinatorial counting, making it both algorithmically interesting and mathematically rich!

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
โ€บ Output: 20
๐Ÿ’ก Note: Valid triplets include all combinations of indices where values sum to 8: (1,2,5), (1,3,4), (2,2,4), etc. Since we have duplicates, we count all possible ways to choose indices, giving us 20 total combinations.
example_2.py โ€” Small Array
$ Input: arr = [1,1,2,2,2,2], target = 5
โ€บ Output: 12
๐Ÿ’ก Note: We can form triplets like (1,2,2) in multiple ways. Since we have 2 ones and 4 twos, the number of ways to pick one 1 and two 2s is C(2,1) ร— C(4,2) = 2 ร— 6 = 12.
example_3.py โ€” No Valid Triplets
$ Input: arr = [1,1,1], target = 10
โ€บ Output: 0
๐Ÿ’ก Note: No three elements can sum to 10 since maximum possible sum is 1+1+1=3. This demonstrates the edge case where no valid combinations exist.

Visualization

Tap to expand
๐Ÿฆ Ice Cream Inventory AnalysisEach flavor appears multiple times - count combinations, not just unique triplets!๐Ÿ“ร—2$1 each๐Ÿฅร—2$2 each๐Ÿฅญร—2$3 each๐Ÿ‡ร—2$4 each๐Ÿร—2$5 eachValid Combination: $1 + $2 + $5 = $8Strawberry + Kiwi + AppleWays to pick: 2 ร— 2 ร— 2 = 8 combinationsC(2,1) ร— C(2,1) ร— C(2,1) = 8Total Valid ArrangementsMultiple valid 3-flavor combos:(1,2,5), (1,3,4), (2,2,4)...Sum all arrangements = 20
Understanding the Visualization
1
Count Inventory
Count how many of each ice cream flavor you have
2
Find Valid Combinations
Identify which 3-flavor combinations meet the target price
3
Calculate Arrangements
For each valid combination, calculate how many ways to pick the scoops
4
Sum All Possibilities
Add up all possible arrangements across all valid combinations
Key Takeaway
๐ŸŽฏ Key Insight: Use frequency counting + combinatorial math C(n,k) to efficiently calculate arrangements of duplicate elements rather than checking every possible triplet!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยณ)

Three nested loops, each potentially running n times in worst case

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using a few variables to track indices and count

n
2n
โœ“ Linear Space

Constraints

  • 3 โ‰ค arr.length โ‰ค 3000
  • 0 โ‰ค arr[i] โ‰ค 100
  • 0 โ‰ค target โ‰ค 300
  • Return answer modulo 109 + 7
Asked in
Google 38 Amazon 45 Meta 29 Microsoft 33
73.2K Views
Medium-High Frequency
~22 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen