Skip to content

Instantly share code, notes, and snippets.

@amingilani
Last active October 20, 2016 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amingilani/249b732a719bb076592e7a3ca606427c to your computer and use it in GitHub Desktop.
Save amingilani/249b732a719bb076592e7a3ca606427c to your computer and use it in GitHub Desktop.
Codes for Hacker Rank 30 days of code
# Code required to read in the values of k,n and candies.
n = gets.to_i
k = gets.to_i
candy = Array.new(n)
for i in 0..n - 1
candy[i] = gets.to_i
end
sorted_candies = candy.sort.to_a
fairness = sorted_candies
.each_with_index
.map { |e, i| sorted_candies[i + k - 1].nil? ? nil : sorted_candies[i + k - 1] - e }
.compact
.min
ans = fairness
puts ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment