From c88d7ce1a8a14638c9f5d5f7c421f183adaea532 Mon Sep 17 00:00:00 2001 From: Auberon Lopez Date: Sat, 6 Jun 2026 09:35:59 -0700 Subject: [PATCH 1/3] Add Maximum Array Rotation in OCaml --- archive/o/ocaml/maximum-array-rotation.ml | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 archive/o/ocaml/maximum-array-rotation.ml diff --git a/archive/o/ocaml/maximum-array-rotation.ml b/archive/o/ocaml/maximum-array-rotation.ml new file mode 100644 index 000000000..a8110ce5c --- /dev/null +++ b/archive/o/ocaml/maximum-array-rotation.ml @@ -0,0 +1,41 @@ +let ( let* ) = Option.bind + +let init_sum nums = + let _, sum = + Array.fold_left (fun (i, sum) x -> (i + 1, (i * x) + sum)) (0, 0) nums + in + sum + +let best_sum nums = + let len = Array.length nums in + let rec aux best i prev_u prev = + if i = len then best + else + let f = nums.((len - i + 1) mod len) in + let e = nums.(len - i) in + let cand = prev + prev_u - (len * e) + f in + let unweighted = prev_u - e + f in + aux (max best cand) (i + 1) unweighted cand + in + let start = init_sum nums in + let init_sum_unweighted = Array.fold_left ( + ) 0 nums - nums.(0) in + aux start 1 init_sum_unweighted start + +let parse_list list_str = + let rec aux acc l = + match l with + | [] -> Some (List.rev acc) + | head :: rest -> + let* num = head |> String.trim |> int_of_string_opt in + aux (num :: acc) rest + in + list_str |> String.split_on_char ',' |> aux [] + +let parse_args = function [| _; nums |] -> parse_list nums | _ -> None + +let () = + print_endline + @@ + match parse_args Sys.argv with + | Some nums -> nums |> Array.of_list |> best_sum |> string_of_int + | None -> {|Usage: please provide a list of integers (e.g. "8, 3, 1, 2")|} From a4abdaa8e1366ae73eb3343ab074640611913d32 Mon Sep 17 00:00:00 2001 From: Auberon Lopez Date: Sat, 6 Jun 2026 09:59:24 -0700 Subject: [PATCH 2/3] OCaml Max Arr Rot: Simplify by switching to reverse rotation --- archive/o/ocaml/maximum-array-rotation.ml | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/archive/o/ocaml/maximum-array-rotation.ml b/archive/o/ocaml/maximum-array-rotation.ml index a8110ce5c..da590c5bb 100644 --- a/archive/o/ocaml/maximum-array-rotation.ml +++ b/archive/o/ocaml/maximum-array-rotation.ml @@ -1,25 +1,22 @@ let ( let* ) = Option.bind -let init_sum nums = +let weighted_sum nums = let _, sum = Array.fold_left (fun (i, sum) x -> (i + 1, (i * x) + sum)) (0, 0) nums in sum -let best_sum nums = + let best_sum nums = let len = Array.length nums in - let rec aux best i prev_u prev = - if i = len then best + let total = Array.fold_left ( + ) 0 nums in + let rec aux best i prev = + if i > len then best else - let f = nums.((len - i + 1) mod len) in - let e = nums.(len - i) in - let cand = prev + prev_u - (len * e) + f in - let unweighted = prev_u - e + f in - aux (max best cand) (i + 1) unweighted cand + let cand = prev - total + len * nums.(i-1) in + aux (max best cand) (i + 1) cand in - let start = init_sum nums in - let init_sum_unweighted = Array.fold_left ( + ) 0 nums - nums.(0) in - aux start 1 init_sum_unweighted start + let start = weighted_sum nums in + aux start 1 start let parse_list list_str = let rec aux acc l = From 8cd97945835faf0dfd33961a21dcece4b228a892 Mon Sep 17 00:00:00 2001 From: Auberon Lopez Date: Sat, 6 Jun 2026 10:35:15 -0700 Subject: [PATCH 3/3] Ocaml Arr Rot: Refactor and comment --- archive/o/ocaml/maximum-array-rotation.ml | 37 +++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/archive/o/ocaml/maximum-array-rotation.ml b/archive/o/ocaml/maximum-array-rotation.ml index da590c5bb..de7c15b09 100644 --- a/archive/o/ocaml/maximum-array-rotation.ml +++ b/archive/o/ocaml/maximum-array-rotation.ml @@ -1,22 +1,27 @@ let ( let* ) = Option.bind -let weighted_sum nums = - let _, sum = - Array.fold_left (fun (i, sum) x -> (i + 1, (i * x) + sum)) (0, 0) nums - in - sum +(* Returns a tuple of: + - length of list + - unweighted sum of elements + - sum of elements weighted by index *) +let list_sums nums = + List.fold_left + (fun (i, u_sum, w_sum) x -> (i + 1, u_sum + x, (i * x) + w_sum)) + (0, 0, 0) nums - let best_sum nums = - let len = Array.length nums in - let total = Array.fold_left ( + ) 0 nums in - let rec aux best i prev = - if i > len then best - else - let cand = prev - total + len * nums.(i-1) in - aux (max best cand) (i + 1) cand +(* Returns the best weighted sum across any rotation *) +let best_sum nums = + let len, u_sum, w_sum = list_sums nums in + let rec aux best prev_sum nums = + match nums with + | head :: rest -> + (* Shift to the left: element 0 now gets full weight, + all other elements get 1x less weight *) + let cand = prev_sum - u_sum + (len * head) in + aux (max best cand) cand rest + | [] -> best in - let start = weighted_sum nums in - aux start 1 start + aux w_sum w_sum nums let parse_list list_str = let rec aux acc l = @@ -34,5 +39,5 @@ let () = print_endline @@ match parse_args Sys.argv with - | Some nums -> nums |> Array.of_list |> best_sum |> string_of_int + | Some nums -> nums |> best_sum |> string_of_int | None -> {|Usage: please provide a list of integers (e.g. "8, 3, 1, 2")|}