Problem

Given four integer arrays nums1nums2nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

Examples

Example 1:

Input: nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]
Output: 2
Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> nums1[0] + nums2[0] + nums3[0] + nums4[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> nums1[1] + nums2[1] + nums3[0] + nums4[0] = 2 + (-1) + (-1) + 0 = 0

Example 2:

Input: nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]
Output: 1

Solution

Method 1 - Using Hashmap

Here is the approach:

  • HashMap for Sums: Use a HashMap to store the sums of every pair of elements from A and B.
  • Checking Complements: For each pair of elements from nums3 and nums4, check if the negative of their sum exists in the HashMap.
  • Counting Matches: If the sum exists, increment the count by the number of times that sum appears in the HashMap.

Code

Java
public class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();  // HashMap to store sums of elements from nums1 and nums2
        int n = nums1.length;
        int count = 0;  // Counter to keep track of valid quadruples

        // Compute all possible sums of pairs from arrays nums1 and nums2
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int sum = nums1[i] + nums2[j];
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }

        // Compute sums of pairs from arrays nums3 and nums4 and check for complements in the map
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int sum = nums3[i] + nums4[j];
                if (map.containsKey(-sum)) {
                    count += map.get(-sum);  // Increment count by the number of times the complement appears
                }
            }
        }

        return count;  // Return the total count of valid quadruples
    }

    public static void main(String[] args) {
        Solution sol = new Solution();
        int[] nums1 = {1, 2};
        int[] nums2 = {-2, -1};
        int[] nums3 = {-1, 2};
        int[] nums4 = {0, 2};
        int result = sol.fourSumCount(nums1, nums2, nums3, nums4);
        System.out.println("Number of quadruplets: " + result);  // Expected output: 2
    }
}
Python
class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        freq_map = defaultdict(int)  # Dictionary to store sums of elements from nums1 and nums2
        n = len(nums1)
        count = 0  # Counter to keep track of valid quadruples

        # Compute all possible sums of pairs from arrays nums1 and nums2
        for num1 in nums1:
            for num2 in nums2:
                freq_map[num1 + num2] += 1

        # Compute sums of pairs from arrays nums3 and nums4 and check for complements in the dictionary
        for num3 in nums3:
            for num4 in nums4:
                target = -(num3 + num4)
                if target in freq_map:
                    count += freq_map[target]  # Increment count by the number of times the complement appears

        return count  # Return the total count of valid quadruples

Complexity

  • ⏰ Time complexity: O(n^2) since we compute sums of elements pairs for two arrays and then check pairs from the other two arrays.
  • 🧺 Space complexity: O(n^2) for storing sums in the HashMap