Exponential subsets
Practice
4.6 (8 votes)
Algorithms
Dynamic programming
2d dynamic programming
Problem
91% Success 1299 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given an integer array A consisting of N elements. Your task is to determine for every element if any of its powers can be expressed as the sum of any subset of array A.

Let S be any subset of A.

i=1S.size()Si=A[i]K where K2.

See sample for a better explanation.

If it is possible to do for any element, print 1. Otherwise, print 0.

Input format

  • The first line contains T denoting the number of test cases.
  • The first line of each test case contains an integer N denoting the number of elements in array A.
  • The second line of each test case contains N space-separated integers of array A.

Output format

For each test case, print a single line of N space-separated integers (0/1). Print 0 if there is no subset such that the sum of that subset is equal to any power greater than 1 of the element at this index. Otherwise, print 1.

Constraints

1T5

1N100

1A[i]100i[1,N]

Sample Input
1
8
1 2 3 4 5 6 7 8
Sample Output
1 1 1 1 1 1 0 0
Explanation

Let us consider answers for each index:

1st index: If we choose subset {1} of the array we know 12 =1 so answer for this index is 1 because element at this index 1  can be expressed. Note that  we could also choose higher power.

2nd index: If we choose subset {3,5} of the array we know 23 =8 so answer for this index is 1.

Note that we could also choose power 2 and have set {4} or {1,3}.

3rd index: If we choose subset {1,8} of the array we know 32 =9 so answer for this index is 1.

8th index: There is no subset of array which equals 82,83 or higher powers so answer for this case is 0.

Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
166 votes
Tags:
Dynamic ProgrammingEasyOpenRecursionTwo dimensional
Points:30
8 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingEasyGrammar-VerifiedRecruitTwo dimensional
Points:30
63 votes
Tags:
Dynamic ProgrammingEasyOpenTwo dimensional
Editorial

Login to unlock the editorial