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.
constinsert=(arr,num)=>{leti=arr.length-1;while(i>=0&&arr[i]>num){arr[i+1]=arr[i];i--;}arr[i+1]=num;};constgetMedian=(arr)=>{constmid=Math.floor(arr.length/2);if(arr.length%2===0){return(arr[mid]+arr[mid-1])/2;}returnarr[mid];};functionrunningMedian(a){// Write your code hereletresult=[];letarr=[];for(leti=0;i<a.length;i++){insert(arr,a[i]);result.push(getMedian(arr));}returnresult;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find the Running Median
You are viewing a single comment's thread. Return to all comments →
JavaScript Solution