Last active
October 20, 2016 08:30
-
-
Save amingilani/249b732a719bb076592e7a3ca606427c to your computer and use it in GitHub Desktop.
Codes for Hacker Rank 30 days of code
This file contains 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
# 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