You are viewing a single comment's thread. Return to all comments →
public static List<int> closestNumbers(List<int> arr) { arr.Sort(); List<int> res = new(); int diff = int.MaxValue; for(int i = 0; i < arr.Count-1; i++) { int temp = Math.Abs(arr[i] - arr[i+1]); if(temp < diff) { diff = temp; res.Clear(); } if(temp == diff) { res.Add(arr[i]); res.Add(arr[i+1]); } } return res; }
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 →
C# Solution