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
Visualization
Time & Space Complexity
Three nested loops, each potentially running n times in worst case
Only using a few variables to track indices and count
Constraints
- 3 โค arr.length โค 3000
- 0 โค arr[i] โค 100
- 0 โค target โค 300
- Return answer modulo 109 + 7