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.
/*
* Complete the 'closestNumbers' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
public static List<Integer> closestNumbers(List<Integer> arr) {
Collections.sort(arr);
int mini = Integer.MAX_VALUE;
for(int i=1;i<arr.size();i++){
if(Math.abs(arr.get(i)-arr.get(i-1))<mini){
mini = Math.abs(arr.get(i)-arr.get(i-1));
}
}
List<Integer> result = new LinkedList<>();
for(int i=1; i<arr.size(); i++){
if(Math.abs(arr.get(i)-arr.get(i-1))==mini){
result.add(arr.get(i-1));
result.add(arr.get(i));
}
}
return result;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Numbers
You are viewing a single comment's thread. Return to all comments →
class Result {
}