> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# 3Sum With Multiplicity Python Solution

> Tested Python solution for LeetCode 923 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 923, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Two Pointers](/catalog/topics/two-pointers), [Sorting](/catalog/topics/sorting), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/three-sum-multiplicity/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 923   # by problem number
lcpy gen -s three_sum_multiplicity   # by problem name
```

## Problem

Given an integer array `arr`, and an integer `target`, return the number of tuples `i, j, k` such that `i < j < k` and `arr[i] + arr[j] + arr[k] == target`.

As the answer can be very large, return it **modulo** `10^9 + 7`.

### Examples

```
Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation:
Enumerating by the values (arr[i], arr[j], arr[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.
```

```
Input: arr = [1,1,2,2,2,2], target = 5
Output: 12
Explanation:
arr[i] = 1, arr[j] = arr[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.
```

```
Input: arr = [2,1,3], target = 6
Output: 1
Explanation: (1, 2, 3) occured one time in the array so we return 1.
```

### Constraints

* `3 <= arr.length <= 3000`
* `0 <= arr[i] <= 100`
* `0 <= target <= 300`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/three_sum_multiplicity/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/three_sum_multiplicity/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter
from math import comb


class Solution:
    # Time: O(n + V^2) where V = 101 distinct values
    # Space: O(V)
    def three_sum_multiplicity(self, arr: list[int], target: int) -> int:
        mod = 10**9 + 7
        count = Counter(arr)
        values = sorted(count)
        total = 0
        for i, x in enumerate(values):
            for y in values[i:]:
                z = target - x - y
                if z < y or z not in count:
                    continue
                if x == y == z:
                    total += comb(count[x], 3)
                elif x == y:
                    total += comb(count[x], 2) * count[z]
                elif y == z:
                    total += comb(count[y], 2) * count[x]
                else:
                    total += count[x] * count[y] * count[z]
        return total % mod
```

## Complexity

| Time | Space |
| - | - |
| O(n + V^2) where V = 101 distinct values | O(V) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.