Skip to content

Instantly share code, notes, and snippets.

@hygull
Created January 9, 2017 06:50
Show Gist options
  • Select an option

  • Save hygull/06666cc316c36e816c93371adc1ed988 to your computer and use it in GitHub Desktop.

Select an option

Save hygull/06666cc316c36e816c93371adc1ed988 to your computer and use it in GitHub Desktop.
Hackerearth (sum of numbers problem) created by hygull - https://repl.it/FDbh/2
"""
coded_on : 9 Jan 2017.
coded_by : Rishikesh Agrawani.
problem link : https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/practice-problems/algorithm/sum-of-numbers-9/
aim_of_script: Sum of numbers problem on Hackerearth.
"""
testcases = int(raw_input()) #testcases
for t in xrange(testcases):
n = int(raw_input()) #No. of elements in an array/list
nums_set = [int(num) for num in raw_input().split() ]#eg. [2,3,4,0,1]
s = int(raw_input())
if s==0:
print "YES"
continue
combinations=2**n #Calculating cube(2,n)
ok=False
for i in xrange(combinations):
l=[]
for j in xrange(n):
if (i>>j)&1 ==1:
l.append(nums_set[j]) #Appending the individual elements of a member of power set of entered list of numbers to a list
addition=0
for item in l:
addition+=item
if addition==s: #If the addition equals s then we achieved the goal...so break
ok=True
break
if ok:
break
if ok:
print "YES"
else:
print "NO"
""" RUN:-
1
5
1 2 3 4 5
5
YES
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment