LeetCodee

923. 3Sum With Multiplicity

Jump to Solution: Python Java C++ JavaScript C#

Problem Description

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.

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.

Constraints:

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

Solution

Python Solution

class Solution:
    def threeSumMulti(self, arr: List[int], target: int) -> int:
        MOD = 10**9 + 7
        count = [0] * 101
        for x in arr:
            count[x] += 1
            
        ans = 0
        for i in range(101):
            if count[i] == 0:
                continue
            for j in range(i, 101):
                if count[j] == 0:
                    continue
                k = target - i - j
                if k < j or k > 100:
                    continue
                if count[k] == 0:
                    continue
                    
                if i == j == k:
                    ans += count[i] * (count[i] - 1) * (count[i] - 2) // 6
                elif i == j:
                    ans += count[i] * (count[i] - 1) // 2 * count[k]
                elif j == k:
                    ans += count[i] * count[j] * (count[j] - 1) // 2
                else:
                    ans += count[i] * count[j] * count[k]
                    
        return ans % MOD

Time Complexity: O(n + 100^2)

We first count frequencies in O(n), then iterate through possible pairs in O(100^2).

Space Complexity: O(1)

We use a fixed-size array of size 101.

Java Solution

class Solution {
    public int threeSumMulti(int[] arr, int target) {
        long[] count = new long[101];
        for (int x : arr) {
            count[x]++;
        }
        
        long ans = 0;
        for (int i = 0; i <= 100; i++) {
            if (count[i] == 0) continue;
            for (int j = i; j <= 100; j++) {
                if (count[j] == 0) continue;
                int k = target - i - j;
                if (k < j || k > 100) continue;
                if (count[k] == 0) continue;
                
                if (i == j && j == k) {
                    ans += count[i] * (count[i] - 1) * (count[i] - 2) / 6;
                } else if (i == j) {
                    ans += count[i] * (count[i] - 1) / 2 * count[k];
                } else if (j == k) {
                    ans += count[i] * count[j] * (count[j] - 1) / 2;
                } else {
                    ans += count[i] * count[j] * count[k];
                }
            }
        }
        
        return (int)(ans % (1e9 + 7));
    }
}

Time Complexity: O(n + 100^2)

We first count frequencies in O(n), then iterate through possible pairs in O(100^2).

Space Complexity: O(1)

We use a fixed-size array of size 101.

C++ Solution

class Solution {
public:
    int threeSumMulti(vector& arr, int target) {
        long count[101] = {0};
        for (int x : arr) {
            count[x]++;
        }
        
        long ans = 0;
        for (int i = 0; i <= 100; i++) {
            if (count[i] == 0) continue;
            for (int j = i; j <= 100; j++) {
                if (count[j] == 0) continue;
                int k = target - i - j;
                if (k < j || k > 100) continue;
                if (count[k] == 0) continue;
                
                if (i == j && j == k) {
                    ans += count[i] * (count[i] - 1) * (count[i] - 2) / 6;
                } else if (i == j) {
                    ans += count[i] * (count[i] - 1) / 2 * count[k];
                } else if (j == k) {
                    ans += count[i] * count[j] * (count[j] - 1) / 2;
                } else {
                    ans += count[i] * count[j] * count[k];
                }
            }
        }
        
        return ans % (int)(1e9 + 7);
    }
};

Time Complexity: O(n + 100^2)

We first count frequencies in O(n), then iterate through possible pairs in O(100^2).

Space Complexity: O(1)

We use a fixed-size array of size 101.

JavaScript Solution

/**
 * @param {number[]} arr
 * @param {number} target
 * @return {number}
 */
var threeSumMulti = function(arr, target) {
    const count = new Array(101).fill(0);
    for (const x of arr) {
        count[x]++;
    }
    
    let ans = 0;
    for (let i = 0; i <= 100; i++) {
        if (count[i] === 0) continue;
        for (let j = i; j <= 100; j++) {
            if (count[j] === 0) continue;
            const k = target - i - j;
            if (k < j || k > 100) continue;
            if (count[k] === 0) continue;
            
            if (i === j && j === k) {
                ans += count[i] * (count[i] - 1) * (count[i] - 2) / 6;
            } else if (i === j) {
                ans += count[i] * (count[i] - 1) / 2 * count[k];
            } else if (j === k) {
                ans += count[i] * count[j] * (count[j] - 1) / 2;
            } else {
                ans += count[i] * count[j] * count[k];
            }
        }
    }
    
    return ans % (1e9 + 7);
};

Time Complexity: O(n + 100^2)

We first count frequencies in O(n), then iterate through possible pairs in O(100^2).

Space Complexity: O(1)

We use a fixed-size array of size 101.

C# Solution

public class Solution {
    public int ThreeSumMulti(int[] arr, int target) {
        long[] count = new long[101];
        foreach (int x in arr) {
            count[x]++;
        }
        
        long ans = 0;
        for (int i = 0; i <= 100; i++) {
            if (count[i] == 0) continue;
            for (int j = i; j <= 100; j++) {
                if (count[j] == 0) continue;
                int k = target - i - j;
                if (k < j || k > 100) continue;
                if (count[k] == 0) continue;
                
                if (i == j && j == k) {
                    ans += count[i] * (count[i] - 1) * (count[i] - 2) / 6;
                } else if (i == j) {
                    ans += count[i] * (count[i] - 1) / 2 * count[k];
                } else if (j == k) {
                    ans += count[i] * count[j] * (count[j] - 1) / 2;
                } else {
                    ans += count[i] * count[j] * count[k];
                }
            }
        }
        
        return (int)(ans % (1e9 + 7));
    }
}

Time Complexity: O(n + 100^2)

We first count frequencies in O(n), then iterate through possible pairs in O(100^2).

Space Complexity: O(1)

We use a fixed-size array of size 101.