Algorithm


Problem Name: 454. 4Sum II

Given four integer arrays nums1, nums2, nums3, 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

 

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

 

Constraints:

  • n == nums1.length
  • n == nums2.length
  • n == nums3.length
  • n == nums4.length
  • 1 <= n <= 200
  • -228 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 228

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        Map map = new  HashMap<>();
        for (int i=0; i < A.length; i++) {
            for (int j=0; j < B.length; j++) {
                map.put(A[i] + B[j], map.getOrDefault(A[i] + B[j], 0) + 1);
            }
        }
        
        int ans = 0;
        
        for (int i=0; i < C.length; i++) {
            for (int j=0; j < D.length; j++) {
                ans += map.getOrDefault(-1 * (C[i] + D[j]), 0);
            }
        }
        
        return ans;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]

Output

x
+
cmd
2

#2 Code Example with Javascript Programming

Code - Javascript Programming


const fourSumCount = function(A, B, C, D) {
  const map = new Map()
  let res = 0
  for (let i = 0, clen = C.length; i  <  clen; i++) {
    for (let j = 0, dlen = D.length; j  <  dlen; j++) {
      map.set(
        C[i] + D[j],
        typeof map.get(C[i] + D[j]) == 'undefined'
          ? 1
          : map.get(C[i] + D[j]) + 1
      )
    }
  }
  for (let i = 0, alen = A.length; i  <  alen; i++) {
    for (let j = 0, blen = B.length; j  <  blen; j++) {
      res +=
        typeof map.get((A[i] + B[j]) * -1) == 'undefined'
          ? 0
          : map.get((A[i] + B[j]) * -1)
    }
  }
  return res
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums1 = [1,2], nums2 = [-2,-1], nums3 = [-1,2], nums4 = [0,2]

Output

x
+
cmd
2

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def fourSumCount(self, A: List[int], B: List[int], C: List[int], D: List[int]) -> int:
        ab = collections.Counter([a + b for a in A for b in B])
        return sum(-c - d in ab and ab[-c-d] for c in C for d in D)
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums1 = [0], nums2 = [0], nums3 = [0], nums4 = [0]

Output

x
+
cmd
1

#4 Code Example with C# Programming

Code - C# Programming

start coding...
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
#453 Leetcode Minimum Moves to Equal Array Elements Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#455 Leetcode Assign Cookies Solution in C, C++, Java, JavaScript, Python, C# Leetcode