Last active
October 31, 2015 11:56
-
-
Save stamaniorec/91c672c8e270cf112b31 to your computer and use it in GitHub Desktop.
#algo #ruby
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
| # 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