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.
intminimumAbsoluteDifference(vector<int>arr){// Sort the array firstsort(arr.begin(),arr.end());// Initialize the minimum difference with a large valueintans=abs(arr[1]-arr[0]);// Compare consecutive elements for the minimum differencefor(inti=1;i<arr.size()-1;i++){ans=min(ans,abs(arr[i+1]-arr[i]));}returnans;}
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 →