Problem 468
Hand of Straights
Medium · 20 pts · Array, Hash Table, Greedy, Sorting
Problem
You hold N cards with integer values and want to arrange all of them into groups of exactly W cards, where each group is W consecutive values (for example 4, 5, 6 when W = 3). Decide whether this is possible. Greedily, the smallest remaining card must start a group, so repeatedly take the smallest card and try to remove the next W - 1 consecutive values.
Input
First line: N and W. Second line: the N card values.
Output
Print YES if the cards can be arranged, otherwise NO.
Constraints
1 <= N <= 10000; 1 <= W <= N; 0 <= value <= 1000000000
Example input
9 3 1 2 3 6 2 3 4 7 8
Example output
YES
Explanation
With W = 3, the cards 1 2 3 6 2 3 4 7 8 can be grouped as [1 2 3], [2 3 4] and [6 7 8], so the answer is YES.