let count_subarr_sum_k arr k =
  let n = Array.length arr in
  let prefix = Array.make (n + 1) 0 in
  for i = 0 to n - 1 do
    prefix.(i + 1) <- prefix.(i) + arr.(i)
  done;
  let count = ref 0 in
  for i = 0 to n - 1 do
    for j = i + 1 to n do
      if prefix.(j) - prefix.(i) = k then count := !count + 1
    done
  done;
  !count
;;

count_subarr_sum_k [| 1; 1; 1; 2; -1; 3; 1; -2; 4 |] 3
