Skip to content

Practice 4Sum II

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#454
LeetCode Problem

4Sum II

Problem Statement

Given four integer arrays nums1, nums2, nums3 and nums4, each of length n, count the index tuples (i, j, k, l) for which nums1[i] + nums2[j] + nums3[k] + nums4[l] equals zero. Each index ranges from 0 through n - 1. Choose one element from each array; different index tuples count separately even when their selected values are equal.

Source: 4Sum II on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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

# 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
-2^28 <= nums1[i], nums2[i], nums3[i], nums4[i] <= 2^28

Related LeetCode Problems

Practice more problems similar to 4Sum II.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.