We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Don't append the diffs. You are just incresing the space complexity. Also you are taking the min(diffs). In the background this is another for loop, so your time complexity increses by O(n).

Just take a variable, maybe name it diffs. Every time you encounter a new min, update the diffs, and finally return it.

bcoz, sorting of array will bring small value element near, thus their diff also min.

suppose if didn't apply sort..
ex.
n = 4
arr = 2 34 68 1
in this case 2 and 1 are not near so their diff will not be calaculate by the loop(as per ur logic).

In this case, if the "list by comprehension" to which you refer is presumably that which is inside the min function, then your assertion is false.

Here in Python3, a generator is being passed to the min function, so no list is being created in memory for it. Elements are being discovered through lazy evaluation of the formula. The minimum is a greedy operation running over the indices i, so it does not store the previous values a[i+1]-a[i] as it proceeds forward. It just finds the minimum up to that point when the i-th term is considered.

least = max(max(arr), min(arr) * -1) * 2
arr = sorted(arr)
for i in range(0, math.ceil(len(arr) / 2)):
f = abs(arr[i] - arr[i + 1])
b = abs(arr[-(i + 1)] - arr[-(i + 2)])
if (least > min(f, b)):
least = min(f, b)
if (least == 0):
return 0
return least

Exactly, this just compares 'neighbors' i.e. numbers at consecutive indices (i, i+1)...that's a set of O(n) whereas the set of all possible pairs (given by, for example, set([i, i for i in range(n)]).symmetric_difference(itertools.product(a, a)) is O(n^2)

Note that the array was sorted beforehand, so that considering differences of consecutive elements is all we need to check to find the minimum difference.
No point in checking the difference between i and i+2 if we know the difference between i and i+1 is definitely less.

## Minimum Absolute Difference in an Array

You are viewing a single comment's thread. Return to all comments →

Yeah, it was pretty easy after reading your comment.

Check for duplicates helps to avoid unnecessary computations.

This is relevant for one of the tests.

Full python solution.

Hackerrank - Minimum Absolute Difference in an Array Solution

good job. also a nice profile.

here is problem solution in

python java c++andCprogramming. https://solution.programmingoneonone.com/2020/07/hackerrank-minimum-absolute-difference-in-an-array-solution.htmlto avoid even more unnecessary computations:

`len(list(set(arr))) -> len(set(arr))`

ha ha! wonderful

You can also abort when diff is zero to pass the test.

Not sure how relevant, this passes all tests:

Don't append the diffs. You are just incresing the space complexity. Also you are taking the min(diffs). In the background this is another for loop, so your time complexity increses by O(n).

Just take a variable, maybe name it diffs. Every time you encounter a new min, update the diffs, and finally return it.

bcoz, sorting of array will bring small value element near, thus their diff also min.

suppose if didn't apply sort.. ex. n = 4 arr = 2 34 68 1 in this case 2 and 1 are not near so their diff will not be calaculate by the loop(as per ur logic).

My

1-line Python3solution (passes all testcases) is similar but no`abs`

or`append`

.The native list sort is done in

`O(n log n)`

time; the naïve approach is`O(n^2)`

.That list by comprehension is equivalent to building it by appending. The space complexity is the same.

In this case, if the "list by comprehension" to which you refer is presumably that which is inside the

`min`

function, then your assertion is false.Here in

Python3, ageneratoris being passed to the`min`

function, so no list is being created in memory for it. Elements are being discovered through lazy evaluation of the formula. The minimum is a greedy operation running over the indices`i`

, so it does not store the previous values`a[i+1]-a[i]`

as it proceeds forward. It just finds the minimum up to that point when the`i`

-th term is considered.Yes, that is right. There is not a list by comprehension but only a generator.

it's really nice!

Full solution explainedHackerrank - Minimum Absolute Difference in an Array Solution

Solution for Beginners

def minimumAbsoluteDifference(arr):

you are not considering all pairs, zip is not useful in this case

Exactly, this just compares 'neighbors' i.e. numbers at consecutive indices (i, i+1)...that's a set of O(n) whereas the set of all possible pairs (given by, for example, set([i, i for i in range(n)]).symmetric_difference(itertools.product(a, a)) is O(n^2)

Note that the array was sorted beforehand, so that considering differences of consecutive elements is all we need to check to find the minimum difference. No point in checking the difference between i and i+2 if we know the difference between i and i+1 is definitely less.

Really spr i got logic