LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDE
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 FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

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