Hello there,
I’m trying to write a function that checks if there exists an element that is strictly the smallest in it’s row and strictly the largest in it’s column.
For example: # let a = [| [|3;2;6;4|]; [|4;5;6;1|]; [|0;3;2;5|] |] # min_max a;;- : bool = false
Should be of the following structure int array array -> bool
Any help is greatly appreciated and thanks in advance.
I gave it a try based on what I understood. The following code is in all likelihood neither good nor efficient.
let getcol a c =
let nr = Array.length a in
let col = Array.make nr 0 in
for i = 0 to (nr-1) do
col.(i) <- a.(i).(c)
done;
col
let get_val_index a cmp =
let n = Array.length a in
let m = ref a.(0) in
let o = ref (Some a.(0), Some 0) in
let duplicate = ref false in
let res=ref 0 in
for i = 1 to (n-1) do
res := (cmp a.(i) !m);
if !res < 0 then
begin
m := a.(i);
o := (Some a.(i), Some i);
duplicate := false;
end
else if !res = 0 then
duplicate := true
else
()
done;
if not !duplicate then !o else (None,None)
let mini a = get_val_index a (compare)
let maxi a = get_val_index a (fun x y -> (-1)*compare x y)
let minmax a =
let r = Array.length a in
let t = ref false in
for i = 0 to (r-1) do
let minr = mini a.(i) in
if minr <> (None, None) then
let maxc = maxi (getcol a (Option.get (snd minr))) in
if maxc <> (None, None) then
if Option.get (fst minr) = Option.get (fst maxc) then t := true;
done;
!t