Four Integers Summing to Zero
InterviewTime limit12sMemory limit1024 MB
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 , , , and , all of the same size.
Write a program that counts the number of tuples such that . Here , , , and are indices from to , choosing one element independently from each of the four arrays.
Input
The first line contains the array size . Each of the next lines contains the integers belonging to , , , and , separated by spaces; the four integers on the -th line are , , , and in that order. The absolute value of every integer is at most .
Output
Print the number of tuples whose sum is .