[back]

Bitonic Sequences

A sequence of integers is bitonic if it is monotonically increasing, monotonically decreasing, or monotonically increasing and then monotonically decreasing, or monotonically decreasing and then monotonically increasing. That is, given \(s_1, s_2, \dots, s_n\), either \(s_1 < \dots < s_n\) or \(s_1 > \dots > s_n\) or there is an index \(i\) such that

\[ s_1 < \dots < s_{i - 1} < s_i > s_{i + 1} > \dots > s_n \] or \[ s_1 > \dots > s_{i - 1} > s_i < s_{i + 1} < \dots < s_n. \]

Implement the function

val is_bitonic : int list -> bool

so that is_bitonic l is true if and only if l is bitonic.

Examples

let _ = assert (is_bitonic [1;2;3;2;1] = true)
let _ = assert (is_bitonic [1;2;3] = true)
let _ = assert (is_bitonic [3;2;1;2] = true)
let _ = assert (is_bitonic [] = true)
let _ = assert (is_bitonic [1;1] = false)
let _ = assert (is_bitonic [1;2;1;2] = false)