Team division

3.8

10 votes
Greedy Algorithms, Algorithms, Basics of Greedy Algorithms, Greedy algorithm
Problem

There are N (where N is always even) players standing in a line where the coordinates of players are given as $$(X1,0), (X2,0)... (XN,0).Youarerequiredtodividethemintotwoteamssuchthatthereisanequalnumberofplayersoneitherside.Forthis,youcanselectacoordinate(P, 0)ifandonlyifthenumberofplayersonthelefthandsideisequaltothenumberofplayersontherighthandside.Fortheplayerson(P,0)$$, you are independent to choose their side.
Find the number of such possible coordinates where teams can be divided.

Note: Non-integral coordinates do not exist.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • For each test case:
    • The first line contains an integer N denoting the number of players.
    • The second line contains an array of space-separated integers denoting array X.

Output format

Print a single line for each test case, denoting the number of coordinates (P,0) from where the teams can be divided.

Constraints

1T20000

1N200000

0|Xi|109

Here, N is always even.

The sum of N over all test cases does not exceed 200000.

Sample Input
2
4
3 -3 1 -2
2
2 2
Sample Output
4
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

First test case:

First test case:

All the players are indicated by an line below the number, all the points which colored black above the number line is suitable for the team division, but we need to consider the only integer co-ordinates, so there are 4 {-2,-1,0,1}.
 

Note that while considering -2,  we can decide which side we want to keep player at -2 and for division we will consider him on left.

Second test case:

Both the players are at 2, so the point we can choose is 2, we can decide to keep one player on left and one player on the right.

Editor Image

?