Under construction

Still being built. Things may change

Longest straight from dice

Interview

Memory limit1024 MB

Summary
Given dice with S_i sides each, find the most dice that can each show one number in a run of consecutive integers.
Level

Medium5 of 10

Topics
Greedy, Sorting
Solved
No attempts yet

Problem

The most common die has 6 sides, each showing a different integer from 1 to 6. Many games use other kinds of dice too. A dkdk is a die with kk sides, each showing a different integer from 1 to kk. A d6d6 is a typical die, a d4d4 has four sides, and a d1000000d1000000 has one million sides.

We start with a collection of NN dice. The ii-th die is a dSidS_i, meaning it has SiS_i sides showing the integers 1 through SiS_i. A straight of length ℓ\ell starting at xx is the list of integers x,x+1,…,x+(ℓ−1)x, x+1, \dots, x+(\ell-1). We choose some of the dice (possibly all) and pick one number from each to form a straight. What is the longest straight we can form?

Input

The first line contains the number of test cases TT. Each test case is given in two lines. The first line contains a single integer NN, the number of dice. The second line contains NN integers S1,S2,…,SNS_1, S_2, \dots, S_N, where each integer is the number of sides of a different die.

Output

For each test case, output one line in the format Case #x: y, where xx is the test case number (starting from 1) and yy is the maximum number of dice that can be used in a straight.

Limits

1≤T≤1001 \le T \le 100.

Hints

In Sample Case #1, there are multiple ways to form a straight using all 4 dice. One possible way is shown in the image above.

In Sample Case #2, no die can show an integer greater than 5, so no straight can use more than 5 dice. There are multiple ways to form a straight with exactly 5 dice. For example, pick 4 and 5 from both d5 dice, then pick 1, 2, and 3 from three of the d4 dice to form 1, 2, 3, 4, 5.

In Sample Case #3, the straight 1, 2, 3, 4, 5, 6, 7, 8, 9 is possible. Discard one d4 and use the remaining d4 dice, the d5, and the d6 to get 1 through 4. Use the d7 dice to get 5 through 7, and the d10 dice to get 8 and 9. No straight of length 10 is possible, so this is the best that can be done.

In Sample Case #4, only a straight of length 1 can be formed, and any integer on the given d10 works.

Examples1

  1. Example 1

    Input
    4
    4
    6 10 12 8
    6
    5 4 5 4 4 4
    10
    10 10 7 6 7 4 4 5 7 4
    1
    10
    
    Expected output
    Case #1: 4
    Case #2: 5
    Case #3: 9
    Case #4: 1