diff --git a/lib/ocaml/baseline/count_subarrays_k.ml b/lib/ocaml/baseline/count_subarrays_k.ml new file mode 100644 index 00000000..ea6b552a --- /dev/null +++ b/lib/ocaml/baseline/count_subarrays_k.ml @@ -0,0 +1,16 @@ +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 diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 7da9235c..76cc379e 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -32,6 +32,7 @@ "coin_min.ml": 6, "count_change.ml": 406, "count_inversions.ml": 12, + "count_subarrays_k.ml": 7, "csv.ml": 10, "egg_drop.ml": 8, "dijkstra.ml": 7, diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 3548ce41..4ead3913 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-11 Phase 5.1 — count_subarrays_k.ml baseline (count + contiguous subarrays of [1;1;1;2;-1;3;1;-2;4] summing to k=3 + = 7). Prefix-sum brute force in O(n²): build cumulative array, + enumerate (i,j) pairs and check prefix[j]−prefix[i]=k. The 7 + witnesses include [1,1,1], [1,1,2,−1], [1,2], [2,−1,3,1,−2], + [−1,3,1], [3], [1,−2,4]. Negative-valued arrays exercise our + evaluator's mixed-sign integer arithmetic (no underflow at the + JS safe-int boundary). 186 baseline programs total. - 2026-05-11 Phase 5.1 — floyd_cycle.ml baseline (Floyd's tortoise -and-hare cycle detection on f(x) = (2x+5) mod 17; μ=0 λ=8 → encoded 8). Three phases: (1) advance slow/fast until they