Arrays and sums

3

15 votes
2D dynamic programming, Algorithms, Data Structures, Dynamic Programming
Problem

You are given an array of elements A1, A2, A3, ..., AN. If you are considering index i, then you contain a set S of all elements whose index is not equal to i. Your task is to find out if you can express Ai as the sum of subsets of S.

For example, you have 8 elements such as 1, 2, 3, 4, 5, 6, 7, 8 and you consider index 3. Therefore, set S is {1, 2, 4, 5, 6, 7, 8} because you have to exclude the element at index i. The answer is Yes because you select subset {1, 2} and its sum is 3. Suppose, if you consider index 8, then set S is {1, 2, 3, 4, 5, 6, 7}. The answer is Yes because you can select {1, 2, 5}, [1, 3, 4}, {2, 6}, or {3, 5}.

Similarly, the answer for i=2 is No because set S is [1, 3, 4, 5, 6, 7, 8} and there is no possible way such that you can select a subset and have sum 2.

Note that you can select no more times than it occurs in set S.

You are given array A. For every index, your task is to determine if it is possible for index i from 1 to N.

Input format

  • The first line contains T denoting the number of test cases.
  • The first line of each test case contains an integer N.
  • The second line of each test case contains N space-separated integers A1, A2, ..., AN.

Output format

For each test case, print a single line consisting of N space-separated integers (0 or 1). Print 0 if it is not possible and 1 if it is possible.

Constraints

1T1000

0Ai1000 i[1,N]

1N10000

Sum of N over all test cases does not exceed 10000

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There is no possible way to obtain 1 and 2. 

For every number greater than 2 it is possible:

Suppose the number is n you can choose 1 and n-1.

Editor Image

?