Minimum Absolute Difference in an Array

  • + 1 comment

    Yes it could be solved in O(n) time, but would require enormous space ~ an array of 2*pow(10,9) locations.

    Take the input temp and store it in a location a[temp+1000000000]th location.Next iterate throough the array and find the closest occuring elements. Also, if any element occurs more than once,print 0.

    To further increase the process you can take min and max values during the input of the temp. Now you would only have to iterate from min to max values present in the array. And if any element's counter crosses 1, break and output 0.

    However, this would not be recommended here due to high space complexity.This has great purposes in other problems.