I have the following piece of code which finds whether any 4 combination in the list sums up to a value(say 7) is possible.If possible it prints 1 else 0. it takes the:- number of list element(n) , list(test) and the number which needs to be checked(say 7)(x here).
Every time I execute it on any online platform it shows "Time limit exceeded". How I can optimize it below 0.87s??Provided I have used "itertools" here.Where I am doing it wrong.
import itertools
t=int(input())
while t!=0:
n=int(input())
test=[int(val) for val in input().strip().split()]
x=int(input())
generator=(itertools.combinations(test,4))
for elem in generator:
val=elem
if sum(val)==x:
flag=1
break
break
if not flag:
print('0')
else:
print('1')
t=t-1