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 <= 3000
0 <= arr[i] <= 100
0 <= target <= 300
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find groups of three numbers that add up to a specific target. The brute force approach simply tries every possible combination of three numbers from the given set.
Here's how the algorithm would work step-by-step:
def three_sum_brute_force(numbers, target): count = 0
array_length = len(numbers)
for first_number_index in range(array_length):
for second_number_index in range(first_number_index + 1, array_length):
# Ensures we don't repeat pairs by only considering later indices
for third_number_index in range(second_number_index + 1, array_length):
# Prevents duplicate triplets.
if numbers[first_number_index] + numbers[second_number_index] + numbers[third_number_index] == target:
# Increment the count when a valid triplet is found.
count += 1
return count
The efficient solution avoids checking every possible combination. It counts the frequency of each number to quickly calculate how many valid triplets exist, significantly reducing the computation needed.
Here's how the algorithm would work step-by-step:
from collections import Counter
def three_sum_multiplicity(array, target):
frequency = Counter(array)
keys = sorted(frequency.keys())
mod = 10**9 + 7
answer = 0
for i in range(len(keys)):
first_number = keys[i]
for j in range(i, len(keys)):
second_number = keys[j]
third_number_needed = target - first_number - second_number
if third_number_needed in frequency and third_number_needed >= second_number:
third_number = third_number_needed
frequency_first = frequency[first_number]
frequency_second = frequency[second_number]
frequency_third = frequency[third_number]
# All 3 numbers are the same
if first_number == second_number == third_number:
# Use combinations formula nCr = n! / (r! * (n-r)!) where r = 3
answer += (frequency_first * (frequency_first - 1) * (frequency_first - 2) // 6)
# Only the first and second numbers are the same
elif first_number == second_number:
# Use combinations formula nCr = n! / (r! * (n-r)!) where r = 2
answer += (frequency_first * (frequency_first - 1) // 2) * frequency_third
# Only the second and third numbers are the same
elif second_number == third_number:
answer += frequency_first * (frequency_second * (frequency_second - 1) // 2)
# All 3 numbers are different
else:
answer += frequency_first * frequency_second * frequency_third
answer %= mod
# Avoids returning negative number
return answer
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately as no triplets can be formed. |
Array with fewer than 3 elements | Return 0 immediately because a triplet cannot be formed. |
Large input array with potential integer overflow when calculating counts. | Use modulo arithmetic during count accumulation to prevent overflow. |
Array contains all identical numbers | Handle this case by calculating combinations directly based on the element's count and target value. |
Array contains negative numbers | The hashmap and two-pointer approaches will correctly account for negative numbers. |
Array contains zeros | Zeros can be valid numbers in a triplet, so include them in calculations. |
Target value is very large or small, such that no valid triplet exists. | The algorithm should naturally result in a count of 0 when no combination adds up to the target. |
Large count of one specific number that causes integer overflow when calculating combination possibilities | Apply modulo to the intermediate calculation to prevent overflow. |