let twosum xs target =
  let h = Hashtbl.create 8 in
  let result = ref (-1, -1) in
  List.iteri (fun i x ->
    let need = target - x in
    match Hashtbl.find_opt h need with
    | Some j -> result := (j, i)
    | None -> Hashtbl.add h x i
  ) xs;
  !result

;;

let (i1, j1) = twosum [2;7;11;15] 9 in
let (i2, j2) = twosum [3;2;4] 6 in
let (i3, j3) = twosum [3;3] 6 in
i1 + j1 + i2 + j2 + i3 + j3
