Last active
January 30, 2018 00:35
-
-
Save Trshant/d0c3e2b67f2203e580a79c434a758074 to your computer and use it in GitHub Desktop.
### [Challenge Name: Minimum Absolute Difference in an Array](/challenges/minimum-absolute-difference-in-an-array) Consider an array of integers, $A = a_0, a_1, \ldots, a_{n-1}$. We define the [absolute difference](https://en.wikipedia.org/wiki/Absolute_difference) between two elements, $a_i$ and $a_j$ (where $i \ne j$), to be the [absolute value](
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/ruby | |
def minimumAbsoluteDifference(n, arr) | |
b = arr.sort! | |
c = b.map.with_index {|a,i| a - (b[i+1]||0) } | |
d = c.map( &:abs ) | |
return d.sort[0] | |
end | |
n = gets.strip.to_i | |
arr = gets.strip | |
arr = arr.split(' ').map(&:to_i) | |
result = minimumAbsoluteDifference(n, arr) | |
puts result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment