Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 109 + 7.
Example 1:
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.
Example 2:
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.
Example 3:
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 <= 30000 <= arr[i] <= 1000 <= target <= 300Problem summary: 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 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers
[1,1,2,2,3,3,4,4,5,5] 8
[1,1,2,2,2,2] 5
[2,1,3] 6
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #923: 3Sum With Multiplicity
class Solution {
public int threeSumMulti(int[] arr, int target) {
final int mod = (int) 1e9 + 7;
int[] cnt = new int[101];
for (int x : arr) {
++cnt[x];
}
int n = arr.length;
int ans = 0;
for (int j = 0; j < n; ++j) {
--cnt[arr[j]];
for (int i = 0; i < j; ++i) {
int c = target - arr[i] - arr[j];
if (c >= 0 && c < cnt.length) {
ans = (ans + cnt[c]) % mod;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #923: 3Sum With Multiplicity
func threeSumMulti(arr []int, target int) (ans int) {
const mod int = 1e9 + 7
cnt := [101]int{}
for _, x := range arr {
cnt[x]++
}
for j, b := range arr {
cnt[b]--
for _, a := range arr[:j] {
if c := target - a - b; c >= 0 && c < len(cnt) {
ans = (ans + cnt[c]) % mod
}
}
}
return
}
# Accepted solution for LeetCode #923: 3Sum With Multiplicity
class Solution:
def threeSumMulti(self, arr: List[int], target: int) -> int:
mod = 10**9 + 7
cnt = Counter(arr)
ans = 0
for j, b in enumerate(arr):
cnt[b] -= 1
for a in arr[:j]:
c = target - a - b
ans = (ans + cnt[c]) % mod
return ans
// Accepted solution for LeetCode #923: 3Sum With Multiplicity
struct Solution;
const MOD: usize = 1_000_000_007;
impl Solution {
fn three_sum_multi(a: Vec<i32>, target: i32) -> i32 {
let mut count = vec![0; 101];
for x in a {
count[x as usize] += 1;
}
let mut res = 0;
for x in 0..101 {
for y in x + 1..101 {
if x + y >= target as usize {
break;
}
for z in y + 1..101 {
if x + y + z > target as usize {
break;
}
if x + y + z == target as usize {
res += count[x] * count[y] * count[z];
res %= MOD;
}
}
}
}
for x in 0..101 {
for y in x + 1..101 {
if x + x + y != target as usize {
continue;
}
if count[x] > 1 {
res += count[x] * (count[x] - 1) / 2 * count[y];
res %= MOD;
}
}
}
for x in 0..101 {
for y in x + 1..101 {
if x + y + y != target as usize {
continue;
}
if count[y] > 1 {
res += count[x] * count[y] * (count[y] - 1) / 2;
res %= MOD;
}
}
}
for x in 0..101 {
if x + x + x != target as usize {
continue;
}
if count[x] > 2 {
res += count[x] * (count[x] - 1) * (count[x] - 2) / 6;
res %= MOD;
}
}
res as i32
}
}
#[test]
fn test() {
let a = vec![1, 1, 2, 2, 3, 3, 4, 4, 5, 5];
let target = 8;
let res = 20;
assert_eq!(Solution::three_sum_multi(a, target), res);
let a = vec![1, 1, 2, 2, 2, 2];
let target = 5;
let res = 12;
assert_eq!(Solution::three_sum_multi(a, target), res);
}
// Accepted solution for LeetCode #923: 3Sum With Multiplicity
function threeSumMulti(arr: number[], target: number): number {
const mod = 10 ** 9 + 7;
const cnt: number[] = Array(101).fill(0);
for (const x of arr) {
++cnt[x];
}
let ans = 0;
const n = arr.length;
for (let j = 0; j < n; ++j) {
--cnt[arr[j]];
for (let i = 0; i < j; ++i) {
const c = target - arr[i] - arr[j];
if (c >= 0 && c < cnt.length) {
ans = (ans + cnt[c]) % mod;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.