Design Gurus Logo
4Sum II (medium)

Problem Statement

Given four arrays of integers nums1, nums2, nums3, and nums4 of the same length n, return the count 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], nums2 = [-1], nums3 = [0], nums4 = [0]
  • Expected Output: 1
  • Justification: The one quadruplet that sum up to 0 is:
    • (1, -1, 0, 0)

Example 2:

  • Input: nums1 = [0, 1], nums2 = [1, -1], nums3 = [-1, 0], nums4 = [1, -1]
  • Expected Output: 4
  • Justification: The four quadruplets that sum up to 0 are:
    • (0, 1, 0, -1)
    • (0, -1, 0, 1)
    • (1, -1, -1, 1)
    • (1, 1, -1, -1)

Example 3:

  • Input: nums1 = [-1, 3], nums2 = [2, -3], nums3 = [3, 4], nums4 = [-4, -3]
  • Expected Output: 3
  • Justification: The three quadruplets that sum up to 0 are:
    • (-1, 2, 3, -4)
    • (3, -3, 4, -4)
    • (3, -3, 3, -3)

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory