Skip to content

Instantly share code, notes, and snippets.

@stamaniorec
Last active October 31, 2015 11:56
Show Gist options
  • Select an option

  • Save stamaniorec/91c672c8e270cf112b31 to your computer and use it in GitHub Desktop.

Select an option

Save stamaniorec/91c672c8e270cf112b31 to your computer and use it in GitHub Desktop.
#algo #ruby
# Remove every subsequence of K elements with the same value.
# 1 1 1 2 3 3 4 5
# 2
# Output: 1 2 4 5
sequence = $stdin.readline.chomp.split.map { |i| i.to_i }
k = $stdin.gets.chomp.to_i
last_digit = sequence[0]
count = 1
sequence[1..-1].each do |digit|
if digit != last_digit
(count % k).times { print "#{last_digit} " }
last_digit = digit
count = 1
else
count += 1
end
end
(count % k).times { print "#{last_digit} " }
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment