algorithm - find the smallest e1-e2 in a set of many elements in Java language -
i have problem is: given set of integers (e1,e2,e3....) , determine smallest ex-ey (the smallest result of subtraction of 2 elements in set). know has algorithm have no knowledge that. can me giving logics or code in java. many thanks!
the optimal solution can think of sort set ( o(n log n) ) , perform pairwise comparison on each consecutive pair in set ( o(n) ).
the "naive" algorithm compares every element against every other element o(n^2).
Comments
Post a Comment