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.
// sort array
arr.sort((a, b) => a - b)
// consider a min
let min = Math.abs(arr[0] - arr[1])
for(let i = 1; i < arr.length - 1; i++) {
// calculate absolute between absolutes
// minimum difference is between adjacent elements in a sorted array
let m = Math.abs(arr[i] - arr[i+1])
if(m < min) {
min = m
}
}
return min
}
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 solution:
function minimumAbsoluteDifference(arr) {
}