3Sum With Multiplicity

MediumTwo Pointershashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array arr and an integer target, count the index triples (i, j, k) with i < j < k and arr[i] + arr[j] + arr[k] == target. Triples are told apart by their indices, so equal values at different positions form different triples. The count can be very large, so return it modulo 10^9 + 7.

Examples

Input: arr = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Value patterns (1,2,5), (1,3,4), (2,2,4) and (2,3,3) contribute 8, 8, 2 and 2 index triples.
Input: arr = [1,1,2,2,2,2], target = 5
Output: 12
Choose one of the two 1s and two of the four 2s: 2 * 6 = 12.
Input: arr = [2,1,3], target = 6
Output: 1
The only triple uses all three elements.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems