Three Sum With Multiplicity
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
Example 1:
Example 2:
Note
固定一个数,做2sum
The tricky parts are two:
if the two sum all the elements are the same, then we count the length * (length - 1) / 2 like [2,2,2,2] is 6 ways
otherwise, we track the duplicate left or right elements and get the product of them
Code
Last updated