Created
October 26, 2021 18:33
-
-
Save VahidDopler/25ac03312cb29965faf7557e54c9c366 to your computer and use it in GitHub Desktop.
Find minimum aand max in javascript
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
'use strict' | |
function findMinMax(array , start , end ) | |
{ | |
debugger; | |
var max , min; | |
if (start == end) | |
{ | |
max = array[start]; | |
min = array[start]; | |
} | |
else if (start +1 == end) | |
{ | |
if (array[start] < array[end]) | |
{ | |
max = array[end]; | |
min = array[start]; | |
} | |
else | |
{ | |
max = array[start]; | |
min = array[end]; | |
} | |
} | |
else | |
{ | |
var mid = Math.trunc(start + (end - start)/2); | |
var left = []; | |
left = findMinMax(array , start , mid); | |
var right = []; | |
right = findMinMax(array , mid+1 , end); | |
if (left[0] > right[0]) | |
max = left[0]; | |
else | |
max = right[0]; | |
if (left[1] < right[1]) | |
min = left[1]; | |
else | |
min = right[1]; | |
} | |
var ans = [max , min]; | |
return ans; | |
} | |
const arr = [3 , 7 , 9 , 2 , 1 , 8]; | |
console.log(findMinMax(arr , 0 , arr.length-1)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment