You are viewing a single comment's thread. Return to all comments →
First sort the array with quick sort which takes nlogn time, then apply
Long diff = Long.MAX_VALUE; String output = "": for(int i=0;i<n-1;i++) if(a[i+1]-a[i] <= diff) diff = a[i+1]-a[i]; for(int i=0;i<n-1;i++) if(a[i+1]-a[i]==diff) output += a[i] + " " +a[i+1]+" "; System.out.println(output);
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 →
in approx O(nlogn + 2n) time
First sort the array with quick sort which takes nlogn time, then apply