Sherlock and MiniMax Discussions | Algorithms | HackerRank
We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionsherlockAndMinimax(arr,p,q){// Write your code hereletmaxInd=p;letminimum=Number.MAX_SAFE_INTEGER;arr.sort((a,b)=>a-b);for(constelementofarr){constabsP=Math.abs(element-p);if(absP<minimum){minimum=absP;}}letmaximum=minimum;minimum=Number.MAX_SAFE_INTEGER;for(leti=1;i<=arr.length;i++){constmid=parseInt((arr[i]+arr[i-1])/2,10);if(p<mid&&mid<q){minimum=Math.min(mid-arr[i-1],arr[i]-mid);if(minimum>maximum){maximum=minimum;maxInd=mid;}}}minimum=Number.MAX_SAFE_INTEGER;for(constelementofarr){constabsQ=Math.abs(element-q);if(absQ<minimum){minimum=absQ;}}if(minimum>maximum){maximum=minimum;maxInd=q;}returnmaxInd;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and MiniMax
You are viewing a single comment's thread. Return to all comments →