Chef has an array
of length consisting of and only.
In one operation, Chef can choose any index and multiply the element by .
Find the minimum number of operations required to make the sum of the array equal to . Output -1
if the sum of the array cannot be made .
Input Format
- First line will contain , number of test cases. Then the test cases follow.
- First line of each test case consists of a single integer denoting the length of the array.
- Second line of each test case contains space-separated integers denoting the array .
Output Format
For each test case, output the minimum number of operations to make the sum of the array equal to . Output -1
if it is not possible to make the sum equal to .
Constraints
- or
Sample Input 1
4
4
1 1 1 1
5
1 -1 1 -1 1
6
1 -1 -1 1 1 1
2
1 -1
Sample Output 1
2
-1
1
0
Explanation
Test case : The minimum number of operations required is . In the first operation, change from to . Similarly, in the second operation, change from to . Thus, the sum of the final array is .
Test case : It can be proven that the sum of the array cannot be made equal to zero by making any number of operations.
Test case : We can change from to in one operation. Thus, the sum of the array becomes .
Test case : The sum of the array is already zero. Thus we do not need to make any operations.
No comments:
Post a Comment