#K86802. Count Straights in a Deck of Cards

    ID: 36945 Type: Default 1000ms 256MiB

Count Straights in a Deck of Cards

Count Straights in a Deck of Cards

You are given a deck of cards containing n integers, where duplicates may appear. First, remove the duplicate values and sort the resulting set of card values. A straight is defined as any contiguous sequence of at least 3 consecutive integers. For a consecutive block of length L (with L \ge 3), the number of straights contained in that block is given by the formula \(L - 2\). Your task is to compute the total number of distinct straight sequences present in the deck.

Example: For the card values 4 5 6 7 8 12 13, after removing duplicates the sorted sequence is 4 5 6 7 8 12 13. The consecutive block 4 5 6 7 8 has length 5 and contributes \(5-2=3\) straights. The remaining cards do not form any straight. Hence, the answer is 3.

inputFormat

The input is given via standard input (stdin). The first line contains an integer n, representing the number of cards. The second line contains n space-separated integers representing the card values.

outputFormat

Output a single integer to standard output (stdout), which is the total number of distinct straight sequences in the deck.## sample

7
4 5 6 7 8 12 13
3