Skip to content

Instantly share code, notes, and snippets.

@miguelraz
Last active January 29, 2022 01:53
Show Gist options
  • Save miguelraz/b2cef9124f6bd64f831b14bc2a61e4ca to your computer and use it in GitHub Desktop.
Save miguelraz/b2cef9124f6bd64f831b14bc2a61e4ca to your computer and use it in GitHub Desktop.
julia> function sol(arr)
# Bail early if too short
length(arr) < 2 && return 0
# Idea - normalize to positive ints and use RadixSort
# then take the maximum of the diff
mini = minimum(arr)
# Use ternary operator `?` and broadcasted in-place addition `.+=`
mini <= 0 ? arr .+= abs(mini) : nothing
# Use in-place RadixSort to modify `arr`
sort!(arr, alg = RadixSort)
# Take the max of the diff
maximum(diff(arr))
end
sol (generic function with 1 method)
julia> sol([3,6,9,1])
3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment