Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Hashing & Two Pointers

Four Arrays Zero Sum

Medium · Hashing & Two Pointers · Asked at Amazon, Google, Microsoft

Given four arrays A, B, C, D of length n each, count the tuples (i, j, k, l) with A[i] + B[j] + C[k] + D[l] = 0.

Input

First line: n. Next four lines: the arrays A, B, C and D, n integers each.

Output

The number of tuples.

Constraints

Meet in the middle: store every A[i] + B[j] in a hash map with its count (n² sums), then for every C[k] + D[l] add the count of −(C[k] + D[l]). The answer can be as large as n^4, so use a 64-bit counter.

Example 1

Input

2
1 2
-2 -1
-1 2
0 2

Output

2

Example 2

Input

1
0
0
0
0

Output

1

Hints

  1. Four nested loops are n^4 = 6 × 10^10 — far too slow.
  2. Split into two halves: A + B and C + D.
  3. Count how often each value of A[i] + B[j] occurs, then look up −(C[k] + D[l]).

Approach

Meet in the middle. Loop over all n² pairs of A and B and count each sum. Then loop over all n² pairs of C and D; each pair completes exactly count[−(C[k] + D[l])] tuples. Since every value is within ±10^6, a pair sum lies in [−2 × 10^6, 2 × 10^6], so a plain array with an offset works as the counter and is faster than a hash map. The answer can reach 500^4 ≈ 6 × 10^10, so the total needs 64 bits.

Complexity: O(n²) time · O(range of sums) space (or O(n²) with a hash map)

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<vector<int>> v(4, vector<int>(n));
    for (auto &arr : v)
        for (auto &x : arr) cin >> x;
    const int OFF = 2000000;
    vector<int> cnt(2 * OFF + 1, 0);
    for (int x : v[0])
        for (int y : v[1]) cnt[x + y + OFF]++;
    long long total = 0;
    for (int x : v[2])
        for (int y : v[3]) {
            int need = -(x + y) + OFF;
            total += cnt[need];
        }
    cout << total << "\n";
    return 0;
}

Python solution

import sys
from collections import Counter

data = sys.stdin.read().split()
n = int(data[0])
A, B, C, D = (list(map(int, data[1 + k * n : 1 + (k + 1) * n])) for k in range(4))
count = Counter(x + y for x in A for y in B)
print(sum(count.get(-(x + y), 0) for x in C for y in D))

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        int[][] v = new int[4][n];
        for (int k = 0; k < 4; k++)
            for (int i = 0; i < n; i++) {
                in.nextToken();
                v[k][i] = (int) in.nval;
            }
        final int OFF = 2000000;
        int[] cnt = new int[2 * OFF + 1];
        for (int x : v[0])
            for (int y : v[1]) cnt[x + y + OFF]++;
        long total = 0;
        for (int x : v[2])
            for (int y : v[3]) total += cnt[-(x + y) + OFF];
        System.out.println(total);
    }
}

Solve Four Arrays Zero Sum online · All practice problems