Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
556 B
9
Indexable
Never
def count_contiguous_subarrays(numbers, k):
    count = 0
    for i in range(len(numbers)):
        freq = {}
        pairs = 0
        for j in range(i, len(numbers)):
            if numbers[j] in freq:
                freq[numbers[j]] += 1
                if freq[numbers[j]] == 2:
                    pairs += 1
            else:
                freq[numbers[j]] = 1
            if pairs >= k:
                count += 1
        if pairs < k:
            break
    return count


numbers = [0,1,0,1,0]
k = 2
print(count_contiguous_subarrays(numbers, k))