from functools import reduce
for _ in range(int(input())):
N = int(input())
l1 = list(map(int,input().split()))
def powerset(lst):
return reduce(lambda result, x: result + [subset + [x] for subset in result],
lst, [[]])
#https://stackoverflow.com/questions/1482308/how-to-get-all-subsets-of-a-set-powerset
lst = (powerset(l1))
print(lst)
ivlst = []
for i in range(1, len(lst)):
ivlst.append((min(lst[i])*max(lst[i])))
print(min(ivlst), end=" ")
print(max(ivlst))
Sample input:
2
2
2 2
3
5 0 9
Sample output:
4 4
0 81
The above code does the following:
- It takes the input as N, where N is the number of elements in the list.
- Then it takes the input as the elements of the list.
- Then it creates a function called powerset which takes a list as an argument and returns all the subsets of that list.
- Then it calls the reduce function on the powerset function with the list as the first argument and the empty list as the second argument.
- The reduce function will return a list of lists.
- The ivlst variable is used to store the values of the minimum and maximum of the subsets.
- Then it iterates over the range from 1 to the length of the list.
- For each iteration, it appends the multiplication of minimum and maximum of the subset to the ivlst list.
- Finally, it prints the minimum and maximum of the ivlst list.
The time complexity is O(2^n) where n is the number of elements in the given set.
I need a way to not use the for loop for getting the min and max values of all sublists, rather I need to get a list containing multiplication of min and max values of all sublists as output from the powerset function itself.