This page is still under construction.

Parts of this page are still being built. What you see may change.

Four Integers Summing to Zero

Interview

Time limit12sMemory limit1024 MB

Summary
Count the number of index tuples (a, b, c, d) with A[a] + B[b] + C[c] + D[d] = 0, given four arrays of size n.
Level

Medium6 of 10

Topics
Hash map, Sorting, Two pointers, Array
Solved
No attempts yet

Problem

You are given four integer arrays AA, BB, CC, and DD, all of the same size.

Write a program that counts the number of tuples (a,b,c,d)(a, b, c, d) such that A[a]+B[b]+C[c]+D[d]=0A[a] + B[b] + C[c] + D[d] = 0. Here aa, bb, cc, and dd are indices from 00 to n−1n-1, choosing one element independently from each of the four arrays.

Input

The first line contains the array size nn (1≤n≤4000)(1 \le n \le 4000). Each of the next nn lines contains the integers belonging to AA, BB, CC, and DD, separated by spaces; the four integers on the ii-th line are A[i]A[i], B[i]B[i], C[i]C[i], and D[i]D[i] in that order. The absolute value of every integer is at most 2282^{28}.

Output

Print the number of tuples (a,b,c,d)(a, b, c, d) whose sum is 00.

Examples4

  1. Example 1

    Input
    6
    -45 22 42 -16
    -41 -27 56 30
    -36 53 -37 77
    -36 30 -75 -46
    26 -38 -10 62
    -32 -54 -6 45
    
    Expected output
    5
    
  2. Example 2

    Input
    1
    0 0 0 0
    
    Expected output
    1
    
  3. Example 3

    Input
    1
    1 1 1 1
    
    Expected output
    0
    
  4. Example 4

    Input
    2
    1 1 1 1
    -1 -1 -1 -1
    
    Expected output
    6