Two elements with maximum difference - PrismoSkills
Problem: Given an unsorted array, find two elements a[i] and a[j] such that i < j and difference between a[i] and a[j] is maximum in the array.For example, in the following array [30, 12, 15, 22, 25, 7, 18], the elements with maximum difference are 12 and 25
Note that neither 12 nor 25 are the maximum or the minimum of the array and yet they have the maximum difference.
A naive solution would be O(n2) where we run 2 loops, comparing each element with all the other elements, finding the difference and then choosing the maximum out of it.
Read full article from Two elements with maximum difference - PrismoSkills
No comments:
Post a Comment