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.
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 that Robert has to add to satisfy the conditions.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤105
1 ≤ c ≤ 106
-x ≤ A[i] ≤ x
for i in range(int(input('Enter the number of test cases\n'))):
print('test #'+str(i+1))
n, x = [int(j) for j in input('Enter n and x\n').split()]
nums = [int(j) for j in input('Enter numbers on cards\n').split()]
res = []
if sum(nums)<0:
k = x
while(abs(sum(res)) != abs(sum(nums))):
if k > abs(sum(nums) + sum(res)):
k -= 1
else:
res.append(k)
print('Necessary cards:')
print(*res)
elif sum(nums)>0:
k = -x
while(abs(sum(res)) != abs(sum(nums))):
if abs(k) > abs(sum(nums) + sum(res)):
k += 1
else:
res.append(k)
print('Necessary cards:')
print(*res)
else:
print('Condition already satisfied')
Example:
Enter the number of test cases
3
test #1
Enter n and x
10 30
Enter numbers on cards
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10
Necessary cards:
30 25
test #2
Enter n and x
10 10
Enter numbers on cards
1 2 3 4 5 6 7 8 9 10
Necessary cards:
-10 -10 -10 -10 -10 -5
test #3
Enter n and x
4 10
Enter numbers on cards
-2 -1 1 2
Condition already satisfied
Comments
Leave a comment