To find tuple (a, b, c, d) with elements from each list, it is straight forward to loop through any two lists to count for partial sum, and then look for opposite partial sum in the rest two lists.

```
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> APlusBcount; int res = 0;
for (int a:A) for (int b:B) APlusBcount[a+b]++;
for (int c:C) for (int d:D) res += APlusBcount[-c-d];
return res;
}
```

The interesting question is how to minimize time complexity if the 4 given lists have different sizes? Let N_{A} ≤ N_{B} ≤ N_{C} ≤ N_{D} be the sizes of the given lists and without losing generality, assume the list sizes are in ascending order. The overall time complexity will be the hashmap size for partial sum count (we pick some list(s) to build) plus the loop(s) to go through the rest list(s) for opposite sum matching, so we have time complexity

- Time =
**O(f(n))**, where**f(n) = max(n, N/n)**with**N := N**,_{A}N_{B}N_{C}N_{D}

where n is the product of list sizes picked to build hashmap. Note that for any 0 < x < y, function f has

**f(x) ≤ f(y)**if and only if**N ≤ xy**,

so for all choices, either **n = N _{D}** or

**N**achieves minimum values. Note that N

_{A}N_{D}_{D}≤ N

_{A}N

_{D}, so we have the optimal time complexity is

**O(f(N**if_{D})) = O(max(N_{D}, N_{A}N_{B}N_{C}))**N**_{B}N_{C}≤ N_{D}

and**O(f(N**,_{A}N_{D})) = O(max(N_{A}N_{D}**N**otherwise._{B}N_{C}))