LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search Functions
Binary Numbers
Binary NumbersNumbers in codeBitwise Operations
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of Eratosthenes
Data Structures
StringsStackQueue
Dynamic Programming
About DPDP problems

Three Sum

EasyProblem #21
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Watch out for overflow when adding elements. Look at the constraints for n

Competitive programmers have a rating, expressed as an integer (possibly negative). A school wants to send three-member teams to the national team contest, and the organizers ask that every team is balanced: the total rating of each team must be zero. Given the ratings of all competitors from one school, your task is to determine in how many ways the school can pick its team.

Formally, you are counting the ways to choose three different competitors whose ratings sum to 0.

Input

The first line contains a single integer t - the number of testcases.

  • The first line of each testcase contains an integer n - the number of competitors.
  • The next line contains n pairwise different integers a0​,a1​,…,an−1​ - their ratings.

Output

For every testcase print a single line with the number of possible teams whose total rating is zero.

Example

Input
1
9
-8 -5 7 4 1 -2 9 -3 2
Output
4

The teams are (−8,1,7), (−5,4,1), (−3,1,2) and (−5,−2,7).

Constraints

1≤t≤100
3≤n≤5000
−109≤ai​≤109
n1​+n2​+…+nt​≤5000


This problem was adapted, with permission, from Trojke datog zbira (3sum), authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit