Split the hand into groups of consecutive values to make the shortest group as long as possible.
Medium5BacktrackingSortingInterviewNo attempts yetTime limit5sMemory limit512 MBYou are playing a card game where every card has one integer written on it.
At the start you receive some cards, your hand. You then split the whole hand into straights, leaving no card out. A straight is a set of cards with consecutive values, for example the three cards {3, 4, 5} or the single card {7}. Once the hand is split you receive a number of dollars equal to the length of the shortest straight. With no cards in hand you can form no straight, so you receive 0 dollars.
Each test case describes the cards in your hand. Find the largest number of dollars you can receive in each test case.
The first line contains the number of test cases T. Each test case is one line. Each line contains N, the number of cards in the hand, followed by the N integers written on those cards, all separated by spaces.
For each test case print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the largest number of dollars you can receive in that test case.
In the first case of the sample input you hold ten cards numbered 1 to 10. One straight of length 10 covers them and pays 10 dollars.
Splitting the second case into {101, 102, 103, 104, 105, 106} and {103, 104} pays 2 dollars. Splitting it into {101, 102, 103, 104} and {103, 104, 105, 106} pays 4 dollars.
The third case has no cards, so it pays 0 dollars.
In the fourth case the card numbered 9 has to sit in a straight of its own, so the answer is 1 dollar.