fork download
  1. x = [3, 2, 3, 3, 2, 8]
  2. k = 8
  3.  
  4. def count_min_length_subarrays(x, k):
  5. prefix_map = {0: [-1]}
  6. current_sum = 0
  7.  
  8. min_len = float('inf')
  9. count = 0
  10.  
  11. for index, value in enumerate(x):
  12. current_sum += value
  13. target = current_sum - k
  14.  
  15.  
  16. if target in prefix_map:
  17. for start_index in prefix_map[target]:
  18. length = index - start_index
  19.  
  20. if length < min_len:
  21. min_len = length
  22. count = 1
  23. elif length == min_len:
  24. count += 1
  25.  
  26.  
  27. if current_sum not in prefix_map:
  28. prefix_map[current_sum] = []
  29. prefix_map[current_sum].append(index)
  30.  
  31. return min_len, count
  32.  
  33. min_length, frequency = count_min_length_subarrays(x, k)
  34. print(f"Minimum Length: {min_length}")
  35. print(f"Count of Minimum Length Subarrays: {frequency}")
  36.  
Success #stdin #stdout 0.11s 14020KB
stdin
Standard input is empty
stdout
Minimum Length: 1
Count of Minimum Length Subarrays: 1