Skip to content

Instantly share code, notes, and snippets.

@adilakhter
Created March 28, 2013 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adilakhter/5259589 to your computer and use it in GitHub Desktop.
Save adilakhter/5259589 to your computer and use it in GitHub Desktop.
(*
* Performs a binary search on the cumulative sums of lengths.
* and return a tuple as (minBound, maxBound)
* : arr: int[] -> n: int -> (int,int)
*)
let getBounds a (n:int): int*int =
let binarySearch (arr:int[]) (n:int) : (int*int) =
let rec binarySearch' (arr:int[]) (n:int) (low:int) (high:int) =
if arr.[high] < n then
(high, System.Int32.MaxValue)
else if high - low <= 1 then
if arr.[low] = n then
(low, low)
else
(low,high)
else
let mid = (high + low)/2
match arr.[mid],n with
| (a,b) when a <= b-> binarySearch' arr n mid high
| _ -> binarySearch' arr n low mid
binarySearch' arr n 0 (arr.Length-1)
binarySearch a n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment