Untitled
unknown
plain_text
4 months ago
1.3 kB
2
Indexable
def count_subset_ways(A): N = len(A) result = [0] * N # For each index for i in range(N): # Numbers to the left of index i left = A[:i] # Numbers to the right of index i right = A[i+1:] # Current element at index i current = A[i] # Count ways to choose subsets ways = 0 # Try all possible subset sizes for subset_size in range(1, len(left) + 1): # Generate all combinations of left subset from itertools import combinations for left_subset in combinations(left, subset_size): # Try all combinations of right subset of same size for right_subset in combinations(right, subset_size): # Combine and check condition combined = list(left_subset) + [current] + list(right_subset) sorted_combined = sorted(combined) # Check if current element is in the middle if sorted_combined[len(sorted_combined)//2] == current: ways += 1 result[i] = ways return result # Test the function A = [3, 1, 2, 4, 5] print(count_subset_ways(A))
Editor is loading...
Leave a Comment