Created
January 30, 2020 11:15
-
-
Save EleDiaz/349061fac5b3bb7c2c843226b64ff68d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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