Skip to content

Instantly share code, notes, and snippets.

@EleDiaz
Created January 30, 2020 11:15
Show Gist options
  • Select an option

  • Save EleDiaz/349061fac5b3bb7c2c843226b64ff68d to your computer and use it in GitHub Desktop.

Select an option

Save EleDiaz/349061fac5b3bb7c2c843226b64ff68d to your computer and use it in GitHub Desktop.
function getMaxMin(array) {
let min
let max = Number.MIN_VALUE
if (array.length > 0) {
min = Math.min(array[0], array[array.length - 1])
}
return { max: getMax(array, max), min: min }
}
function getMax(array, max) {
if (array.length <= 1) {
return Math.max(array[0], max)
}
let initial = array.slice(0, Math.floor(array.length / 2))
let rest = array.slice(Math.ceil(array.length / 2))
if (rest[0] > max) {
return getMax(rest, rest[0])
}
else {
return getMax(initial, max)
}
}
getMax([1, 2, 4, 4, 5], 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment