Sum of cards

2.3

9 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

Robert has a special set of cards to play with. Each card has an integer written on it and the integer can be negative, positive, or zero. The absolute value of the integer on the card cannot exceed x.

Now, Robert misplaced some of the cards and currently has only N cards. He wants to have the sum of integers present on the cards to be zero. 

Find the minimum number of cards that Robert has to add to N cards to make the sum of integers present on all the cards to be zero.

You can assume that Robert has an infinite number of cards with each integer number from x to x.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains two space-separated integers Nx denoting the number of cards and maximum absolute value present on the card.
  • The second line of each test case contains N space-separated integers denoting the integer written on N cards.

Output format

For each test case, print the minimum number of cards which Robert has to add to satisfy the conditions.

Constraints

1T101N1051x106xA[i]x

 

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • Sum of integers on cards is 3.
  • Robert will choose a card with integer -3 written on it. Thus, resulting sum will be 0.
  • Hence, 1 card is required.
Editor Image

?