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.
'usestrict';constfs=require('fs');process.stdin.resume();process.stdin.setEncoding('utf-8');letinputString='';letcurrentLine=0;process.stdin.on('data',function(inputStdin){inputString+=inputStdin;});process.stdin.on('end',function(){inputString=inputString.split('\n');main();});functionreadLine(){returninputString[currentLine++];}/* * Complete the 'minimumAbsoluteDifference' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER_ARRAY arr as parameter. */functionminimumAbsoluteDifference(arr){// Write your code hereletmin=Math.abs(arr[0]-arr[1]);for(leti=0;i<arr.length-1;i++){for(letj=i+1;j<arr.length;j++){constabs=Math.abs(arr[i]-arr[j]);if(abs<min){min=abs;}}}returnmin;}functionmain(){constws=fs.createWriteStream(process.env.OUTPUT_PATH);constn=parseInt(readLine().trim(),10);constarr=readLine().replace(/\s+$/g,'').split(' ').map(arrTemp=>parseInt(arrTemp,10));constresult=minimumAbsoluteDifference(arr);ws.write(result+'\n');ws.end();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Absolute Difference in an Array
You are viewing a single comment's thread. Return to all comments →
JavaScript