We need to read the problem statement carefully.
We have n cards with integers from 1 to n (each integer appears exactly once? Actually "There are n cards with one integer from 1 to n and one blank card." Means originally we have a set of n cards each with a distinct integer from 1 to n, plus a blank card. So total n+1 cards. Among these n+1 cards, we are given k cards (1 ≤ k ≤ n). Actually wait: "Of these n + 1 cards, k cards are given, but 1 ≤ k ≤ n." That implies we are given k cards from the set of n+1. Possibly some of the given cards could be the blank card? The blank card is represented by 0. So among the k given cards, some may be 0 (blank). Also we can write one integer from 1 to n on a blank card (if we have a blank card? Actually we can write one integer from 1 to n on a blank card. The problem says: "You can write one integer from 1 to n on a blank card. I want to make a continuous sequence of integers as long as possible with just a given card." Probably meaning: we have a set of given cards (including possibly a blank). We can optionally write a number on the blank card (if we have a blank). Then we want to select some cards from the given ones (including the blank after writing) to form a contiguous sequence of integers (like 3,4,5,...). The goal is to maximize the length of such a contiguous sequence that can be formed using only the given cards (we can use any subset? But "with just a given card." maybe means using the given cards only? Actually "with just a given card." Might mean using the cards we have (the given ones) without adding any other cards. And we can optionally fill the blank card with a number. So essentially we have a multiset of numbers? Since each integer from 1 to n appears at most once originally, but if the blank is filled, that will produce a number which might duplicate an existing integer? Possibly the blank card is originally blank; we can write any number 1..n, but the original cards already have those numbers. So we could end up with two cards with same number? The problem doesn't explicitly forbid duplicates, but usually you'd not want duplicates because the original set had distinct numbers. However, we are given some cards from that set, maybe we could have the blank and also the card with that number? The blank card can be filled to any number, but if we already have that number card among the given ones, we'd have two cards with the same number. Is that allowed? The goal is to "make a continuous sequence of integers as long as possible" using the cards. Usually you'd need distinct integers to form a sequence. If you have duplicates, you'd still have that integer once. But the problem likely expects that we can use each card at most once; the sequence is defined by the integers on the cards, and they need to be consecutive. So having duplicate numbers doesn't help because you'd only need one copy of each integer. So we would not choose to fill the blank with a number we already have, because it wouldn't extend the sequence beyond using that number. But maybe we don't have that number, so filling can fill a missing number.
Thus we are given k numbers from the set {0,1,2,...,n} where 0 represents blank. We are allowed to replace the 0 with any integer from 1 to n (if there are multiple zeros? Possibly only one blank card exists in total, but we are given k cards from the set; so among given cards, there could be at most one blank because there is only one blank card overall. But could there be more? The statement: "There are n cards with one integer from 1 to n and one blank card. Of these n + 1 cards, k cards are given" So the total set is exactly n+1 distinct cards: n with distinct numbers 1..n, and one blank. So among the given k cards, the blank card might be included or not. So there can be at most one 0 in the input for a dataset. Also k <= n, so we cannot get all n+1 cards, but we could get at most n cards; so we might get the blank or not. But if k <= n, and there are n+1 total, it's possible that the blank is included in the given k. So we have at most one blank.
Thus we have a set S of numbers (some from 1..n) and possibly a blank we can set to any integer we want. We want to select a subset of these numbers (including the filled blank if present) to form a contiguous integer sequence (like L, L+1, ..., R) where each integer appears at least once among our selected cards. But we have a limited number of cards; we can only use each card once. But since we are allowed to choose any subset, and we want to maximize length, we essentially want to find the longest contiguous interval [L,R] such that for each integer i in [L,R], we have at least one card with that number, except possibly one integer that we can supply via the blank (if we have blank). But note: if we have blank, we can fill it with exactly one integer, so we can cover at most one missing number in the interval. Also, we cannot use the blank to cover more than one missing number; it's a single card.
Also, we might have duplicates? Since numbers from 1..n are distinct originally, if we get a blank and fill it with some number that is already present among the given numbers, that would give us two copies, but that's fine; but we would still have that number covered. However, it's wasteful because we could have used the blank to cover a missing number instead. So optimal solution would never fill blank with a number already present, unless all numbers are already present (then blank is irrelevant). So effectively, we have a set of numbers we have (excluding blank). Let's denote T = set of given numbers (non-zero). Let b = 1 if blank is present among given cards, else 0. We can choose any integer x (1..n) to assign to blank if b=1, and then we have T ∪ {x} (if x not in T, else duplicate). But duplicates don't matter; we effectively have T plus optionally we can add one extra number of our choice. Then we want longest consecutive interval [L,R] such that the set T' (the numbers we have after possibly adding x) contains all numbers in that interval. Since we can only add one number, the interval can have at most one missing number from T, and we can fill that with x.
But careful: if we have blank, we can add exactly one number. But we could also choose not to use the blank? But if using blank gives longer sequence, we will use it. So effectively, if b=1, we can cover one missing number.
Thus the problem reduces to: given a set T of integers from 1..n, possibly with size k or k-1 (if blank present), and a flag indicating if we have an extra wildcard (blank) that can cover one missing integer, find the longest contiguous segment [L,R] such that the number of integers missing from T in that segment is at most b (where b is 0 or 1). But also note: we might have the blank but not need to use it; it's okay to have missing count <= b.
Now consider if we have blank? b = 1 if 0 appears in input; else b = 0.
Wait: The input description: "The following k line contains one integer. Written one by one, representing the integers written on the given k cards. Blank cards are represented by 0." So yes, we read k integers; some may be 0 (blank). Since there is exactly one blank card in the full set, we could have at most one 0. But they didn't explicitly say that there cannot be multiple zeros; but logically, only one blank exists, and we are given k cards from the n+1, so at most one blank. So we can safely assume there is at most one 0. But we can also handle multiple if given; but not needed.
Thus our solution: For each dataset, compute max length.
Constraints: n up to 100,000, k up to 100,000, number of datasets ≤5. So total possible values up to 100k. We need O(n) or O(k log k) per dataset.
We need to consider that the set T might have size up to k (if no blank) or k-1 (if blank). But k <= n, so we have at most n numbers.
We need longest contiguous subarray in 1..n where missing numbers count ≤ b. This is like longest segment with at most b gaps.
Approach: We have T (the set of numbers we have). We can sort T (since it's given unsorted maybe). Then we can process sorted list.
Alternatively, we can mark an array of booleans for numbers present. Since n <= 100k, we can use a boolean array of size n+1. However, there may be multiple datasets, but total n per dataset manageable. We'll allocate fresh array for each dataset.
Given presence array present[1..n] (bool). And have blank flag.
Goal: find longest interval [L,R] such that count of i in [L,R] where present[i]==false ≤ blank? Actually we can fill exactly one missing if blank is present. But what if blank=0? Then we need no missing numbers: the interval must be entirely present. That's just longest consecutive run of present numbers.
If blank=1: we can tolerate one missing number; we can fill it with blank. But careful: The blank can be used to cover any missing number, but only one. So within [L,R], we need at most one missing number. However, there is nuance: what if the missing number is outside the interval but we need to use the blank for something else? Actually we are only concerned about covering numbers within the interval. So yes, interval can have at most 1 missing.
But also note: the blank is a separate card; if the interval already has all numbers present, we may still have blank unused; that's fine. So we just need missing count ≤ blank.
Thus problem reduces to: Given binary array of length n, find longest subarray where number of zeros ≤ b (b = 0 or 1). That's classic sliding window.
But is it that straightforward? Let's test with examples.
Example 1:
First dataset:
7 5
6
2
4
7
1
So numbers: 1,2,4,6,7. No blank (0 not present). So present: 1,2,4,6,7. Longest consecutive present? Let's see segments: 1-2 present contiguous length 2; 4 alone length1; 6-7 contiguous length2; so max 2. Output says 2. That matches.
Second dataset:
7 5
6
2
0
4
7
So numbers: 6,2,0,4,7. So present numbers: 2,4,6,7. And blank exists (b=1). So we need longest interval with at most 1 missing. Let's list:
Interval [2,4] includes 2,3,4. present: 2 and 4, missing 3 -> count missing=1 ≤1 => length 3.
Interval [4,6]: 4,5,6 -> present:4,6 missing5 => length3.
Interval [2,6]: 2,3,4,5,6 -> present:2,4,6 missing3,5 => missing=2 >1 not allowed.
Interval [6,7]: 6,7 -> all present length2.
Interval [1,2]: 1,2 -> missing1? present2, missing1 => missing=1 => length2.
Interval [1,3]: 1,2,3 -> missing1,3 => missing2 >1.
Interval [3,7]: 3,4,5,6,7 -> present:4,6,7 missing3,5 => missing2.
But what about interval [2,5]? 2,3,4,5 -> present2,4 missing3,5 -> missing2.
Interval [1,4]? 1,2,3,4 -> present2,4 missing1,3 -> missing2.
What about interval [3,6]? 3,4,5,6 -> present4,6 missing3,5 -> missing2.
So longest length with at most 1 missing is 3. But output says 4. Hmm discrepancy.
Wait output says 4. So perhaps my interpretation is wrong. Let's re-evaluate.
The second dataset has numbers: 6,2,0,4,7. That's 5 cards; among them one blank. So we have cards: 2,4,6,7, and a blank. We can write any integer 1..7 on the blank. The goal is to make a continuous sequence of integers as long as possible using these cards. That means we can select some of these cards (maybe not all) to form a consecutive run. We can write the blank to any number, then use it as that number.
What sequence of length 4 is possible? Let's try: 4,5,6,7? We have 4,6,7 present; we need 5. We have a blank that we can write 5, so we get 4,5,6,7 -> that's length 4. Yes! So we can form [4,7] length 4. In my earlier sliding window approach with present array [2,4,6,7] and blank=1, the interval [4,7] includes numbers 4,5,6,7. Missing numbers: 5 only (since 4,6,7 present). So missing count = 1, okay. Length = 4. I mistakenly thought [4,7] missing count=1? Actually [4,7] = {4,5,6,7}. Present: 4,6,7 -> missing 5. So missing=1, length=4. So why did I earlier say length? I computed [4,7] incorrectly? Let's see: I listed [6,7] length2; I didn't consider [4,7]. Indeed I missed [4,7] because I was considering intervals with start and end in present numbers? But sliding window should find that. So my earlier enumeration didn't include [4,7] because I considered intervals only between present numbers. Actually [4,7] is start=4, end=7, inclusive, length=4. So it's valid. So sliding window approach would find length 4. So my earlier enumeration error; output 4 matches sliding window.
Thus indeed the problem reduces to: Given a set of present numbers (1..n) and a blank (wildcard) that can supply at most one missing number, find the maximum length L such that there exists a contiguous interval where the count of numbers not present ≤ number of blanks (0 or 1). And note: we can use the blank only if we have it; if we have blank, b=1, else b=0.
But there may be nuance: the blank card is an actual card; if we use it to fill a missing number, we still need to have a card for each number in the interval. However, if the interval contains numbers that we have duplicates? That doesn't matter. Also, we could possibly not use some of the given cards; they can be extra. That's fine.
So it's indeed a longest subarray with at most b zeros (where zeros indicate missing numbers) in a binary array of length n indicating presence.
This can be solved by scanning with two pointers, O(n) time.
But wait: n can be up to 100,000, but k can be up to 100,000; however, the array size is n, which could be 100,000, that's fine. But if n is large and k small, we might have many zeros. That's okay.
Edge cases: If blank is present, b=1; but if we also have the blank itself as a card, we treat it as a wildcard that can become any number. However, what if we have blank and also we could use it as a number that is already present? That wouldn't increase length. So in the sliding window, we treat blank as ability to cover one missing. But careful: The blank is a separate card; we cannot use it to cover more than one missing. However, in the sliding window approach, we count missing numbers, and we allow up to b missing. That implicitly assumes we can cover each missing number with a blank? But we have only one blank. So if b=1, we can only cover at most one missing. So condition missing ≤ 1 is correct. But what if the interval includes a number that we don't have, but we also have the blank that we can assign to that number. But what if the interval includes two missing numbers? Then we can't cover both because only one blank. So condition missing ≤ 1 is correct.
What if we have blank, but the interval is completely present? Then we don't need to use blank; it's fine. So missing =0 ≤1.
Thus it's correct.
But wait, there is a subtlety: The blank can be used to represent any integer from 1 to n, but it must be written exactly once. So if we choose to fill it with number x, we then have that card representing x. In an interval that we are considering, if there is a missing number y, we could set blank to y. But if the interval has missing numbers, we must ensure that the blank can be assigned to exactly one missing number; but what if there are multiple missing numbers? Not possible. So indeed at most one missing.
But could there be a scenario where the blank is used to fill a number that is already present, but we use that as a duplicate, allowing us to extend the interval in some other way? That seems pointless because duplicates don't help cover missing numbers. So we can always reassign blank to a missing number if there is one, to maximize interval. So the sliding window condition stands.
Thus solution: For each dataset:
- Read n, k.
- Initialize present[1..n] = false.
- blank = 0 (count of blank cards, should be 0 or 1).
- For i from 1 to k: read x. If x == 0: blank++. else present[x] = true.
- (Optionally check if blank > 1? But should not happen.)
- Now, we want max length of contiguous segment where count of false entries ≤ blank.
- Do sliding window: left = 1, right = 1, missing_count = 0, max_len = 0. While right <= n: include right: if present[right] false: missing_count++. While missing_count > blank: move left: if present[left] false: missing_count--; left++. Update max_len = max(max_len, right-left+1). Then right++.
- This yields O(n) per dataset.
But n can be 100k, that's fine. Number of datasets ≤5, total O(500k). Works.
But need to be careful: Input format: "The following k line contains one integer. Written one by one, representing the integers written on the given k cards." So we read k integers, each on its own line. They are not necessarily distinct? Since they are from distinct cards, they should be distinct numbers except for 0 maybe. But even if duplicate numbers appear due to input error? Not needed. But we can safely set present[x] = true for non-zero, ignoring duplicates.
Edge cases: if blank=0, we need longest contiguous subarray with all present. That is the same as scanning for runs of true.
But is there any scenario where we could use the blank card to fill a number that is outside the interval? Not relevant.
But hold on: the problem statement says "You can write one integer from 1 to n on a blank card. I want to make a continuous sequence of integers as long as possible with just a given card." The phrase "with just a given card." might mean using only the cards we have, without any additional? Already considered.
Thus solution seems straightforward.
But wait, the second sample input: There were two datasets; first dataset gave 2; second gave 4. We matched.
But let's test with some other examples: Suppose n=5, k=4, cards: 1,2,3,4 (no blank). Longest continuous is 4. Should output 4.
Suppose n=5, k=4, cards: 1,2,4,5 (no blank). Longest continuous: 1-2 length2, 4-5 length2 -> output2.
Suppose n=5, k=4, cards: 1,2,3,0 (blank). So present:1,2,3. Blank=1. Longest with at most 1 missing: check intervals: [1,4] length4: numbers 1,2,3,4 -> present 1,2,3 missing4 -> missing=1 ok => length4. [1,5] length5 missing4,5 -> missing=2 not ok. [2,5] length4 missing4,5? Actually 2,3,4,5 present 2,3 missing4,5 -> missing2. So max is 4. Output 4.
What about interval [1,3] length3 all present -> length3 but 4 is longer. So max 4. Could we get 5? To get 5 we need numbers 1-5, missing 4 and5? Actually present 1,2,3, blank can cover one missing, so can't cover both 4 and5. So no.
Suppose we have blank but no numbers? k=1, card 0, n=10. Then present all false, blank=1. We can write any number, so we can only get a sequence of length 1 (just that number). Because we only have one card (the blank). Wait, we have one card, and we can write any number on it. Then we can make a continuous sequence of length 1 (just that number). Could we make longer? No, because we only have one card, we cannot have two numbers. So longest contiguous sequence length is 1. Our sliding window: we have array of length 10 all false, blank=1. Sliding window with at most 1 false: Since all are false, any interval length >1 will have at least 2 false, so not allowed. So max length is 1. That matches.
Suppose we have blank and some numbers, we can possibly use the blank to fill a missing number, but we also need to have cards for all numbers in the interval. The number of cards we have must be at least the length of the interval (since each card can represent one number). Actually we have exactly k cards. So if we want to form an interval of length L, we need to use L cards (each distinct number). We have total cards = k (including blank). But if we have a blank, it becomes a number we assign. So we need L ≤ k, because we can only use at most k cards (each used at most once). However, we may also have extra cards that are not used, that's fine. So the interval length cannot exceed k, because we only have k cards to represent distinct numbers. Wait, is that necessarily? The cards we have are a subset; we can only use each card at most once. To represent a sequence of L distinct numbers, we need at least L cards (one for each number). So L ≤ total number of cards we have = k. So that's an additional constraint.
In the first example, first dataset: n=7, k=5, output 2. Could there be an interval of length 3? We have cards: 1,2,4,6,7 (5 cards). To make a sequence of length 3, we need 3 cards. We have enough cards. But we can't because numbers missing. So length 2. So k is 5, but we couldn't achieve length 5 because numbers missing.
In the second dataset: k=5, we achieved length 4. So L ≤ k holds (4 ≤ 5). Could we achieve length 5? That would require numbers 1..5 or 3..7 etc. With present 2,4,6,7 and blank, we have 5 cards. To get length 5, we need all 5 numbers. Could we set blank to something to get a run of 5? Possibilities: Need 5 consecutive numbers. Let's check if any run of 5 exists: [1,5] missing 3,5? Actually we have 2,4, blank can cover one, so still missing one. [2,6] missing 3,5 (two missing). [3,7] missing 3,5? Actually 3,4,5,6,7: present 4,6,7 missing 3,5 two missing. So not possible. So max 4.
Thus L ≤ k is a necessary condition, but maybe it's automatically considered because the sliding window might produce intervals with length > k if there are many present numbers? But present numbers count is at most k (since we only have k non-blank cards). Actually if blank=0, present count = k. So the number of true entries in the array is exactly k (since each non-zero card is a distinct number). If there is a blank, then present count = k - 1 (since one card is 0). But we also have the blank which can become a number, so effectively we could have up to k numbers present after filling. However, our sliding window approach does not consider the total card count; it only uses the presence array for given numbers. The blank is not represented in the presence array; we only treat it as a wildcard for missing numbers. But we need to ensure that the interval length does not exceed the total number of cards we have. Consider scenario: n=10, k=3, cards: 1,2,3 (no blank). Then present true for 1,2,3. Sliding window with b=0 gives longest contiguous run of true of length 3 (1-3). That's okay, because we have exactly 3 cards. Now consider scenario: n=10, k=3, cards: 1,3,0 (blank). present:1,3; blank=1. Sliding window with b=1: Let's see intervals: [1,3] length3 missing2? Actually numbers 1,2,3: present 1,3 missing 2, so missing=1 => allowed, length=3. But do we have enough cards? We have cards: 1,3, blank. To make sequence 1,2,3 we need three cards: we have card 1, card 3, and we can set blank to 2. That's exactly three cards. So length=3 is feasible. Good.
Now consider scenario: n=10, k=3, cards: 1,5,0. present:1,5; blank=1. Sliding window with b=1: [1,3]? length3 missing2? But we have card 5 not used? We can use only the cards we have. For interval [1,3], we need numbers 1,2,3. We have card 1, blank can be 2, but we need a card for 3. We don't have a card for 3. So we cannot form [1,3] because we only have 2 cards (1 and blank) plus? Actually we have three cards: 1,5, blank. We can assign blank to 2, but we still need a card for 3. We have card 5, but 5 is not 3, and we cannot change it. So we cannot form [1,3]. So our sliding window would incorrectly think [1,3] is possible because presence[3] is false but blank covers one missing, but we don't have a card for 3 at all because we only have cards for 1,5, and blank becomes 2. The presence array only tracks numbers we have cards for (non-blank). In [1,3], there are two missing numbers: 2 and 3. But our blank covers only one missing, so missing count=2 > b=1, so sliding window would reject because missing >1. Actually check: interval [1,3] includes 1 (present true), 2 (false), 3 (false). Missing count = number of false = 2. Since b=1, not allowed. So sliding window would not accept [1,3]. So it's fine.
But consider interval [1,2]: numbers 1 (true), 2 (false). Missing count =1 ≤1, allowed. And we have card 1 and blank->2. That's feasible. So length2.
What about interval [4,6]? 4 false, 5 true, 6 false => missing=2 >1, not allowed. [5,6]? 5 true,6 false missing=1 allowed length2: we need 5,6; we have card5, blank->6, works. So max length maybe 2.
Now consider scenario where we have many present numbers, but limited cards? Actually present count is exactly the number of non-blank cards. So if we have a long interval of consecutive numbers where many are present, but we might not have enough cards to cover the entire interval because we might need to use cards that are present for numbers outside the interval? No, if we choose an interval, we need to use exactly one card for each number in the interval. We can use a present card only if its number is in the interval. If a present card's number is not in the interval, we don't need to use it. So we are free to ignore any extra cards. So the constraint is that the number of cards we have that are either present numbers within the interval or the blank (if used) must be at least the interval length. In other words, the number of cards that can be used for the interval is: count of present numbers that lie in the interval + (blank if we use it). This sum must be at least the interval length. But note: we also need to cover every number in the interval. If we have a present number for a specific number, we use that card. For missing numbers, we need to use blank to cover them. If there is more than one missing, we can't cover because we only have one blank. So the condition missing ≤ blank is necessary. But is it sufficient? Not exactly: we also need to ensure that we have enough cards in total. Suppose we have interval length L, with M missing numbers (numbers not present). To cover these missing numbers, we need to use the blank card M times. But we only have one blank, so M must be ≤ blank. But also, we need to have L - M present numbers that are within the interval. Those present numbers come from our given non-blank cards. We have exactly P = total number of present cards (non-blank) = k - blank. Some of those present cards may lie outside the interval; that's fine, we don't use them. So we only need that the count of present numbers that lie inside the interval is exactly L - M. Since we know the interval's missing count is M, that means the number of present numbers inside the interval is exactly L - M. So we need that there are at least L - M present numbers in the interval, which is automatically true because by definition missing count M = number of numbers in interval that are not present. So the count of present numbers in the interval is L - M. So there is no extra constraint about total cards; as long as we have enough present numbers in the interval to cover those numbers, and we have enough blanks to cover missing ones, it's feasible. But we must also have the blank card available if M > 0. If M = 0, we don't need blank even if we have it. So the only resource constraint is that we have at most one blank. So indeed the condition missing ≤ blank (where blank is 0 or 1) is sufficient.
But there is a nuance: what if we have blank but we also have a present number that is the same as the number we would assign to the blank? Not needed. So fine.
Thus sliding window works.
But wait: is there any scenario where the interval might require using the blank card even if missing=0? No, we can leave it unused.
Thus the problem seems to be exactly that.
But there's also an additional subtlety: the blank card can be written with any integer, but once written, it becomes that integer and cannot be changed. So for a given interval, we assign it to one missing number. That's fine.
Now, the sliding window approach is O(n). But n can be up to 100,000, which is fine.
However, we must be careful with memory and time. Since number of datasets ≤5, total O(5*100k) = 500k, trivial.
But wait: the problem statement says: "The input consists of multiple datasets. Each dataset is given in the following format. The input ends on a line containing two zeros." So typical.
We should output an integer for each dataset.
Edge Cases: Possibly k can be 0? But problem says 1 ≤ k ≤ n, so k at least 1. So there is at least one card.
Also, the blank card is represented by 0. There may be a dataset with k=1 and card 0. That is allowed (since k ≤ n, n≥1). Then present count = 0, blank=1. Our sliding window: array all false, blank=1. Then longest interval with ≤1 missing: any single element interval has missing count either 1 (if false) or 0 (if true, but none true). Actually all false, so for length1 interval, missing=1 ≤1, so valid. For length2, missing=2 >1, so invalid. So max length =1. That's correct.
If k=1 and card non-zero, blank=0. Then present[that] = true. Longest contiguous all present? Only that number true, so longest run of true =1. So output1. That matches.
Now, what about n=1? n can be 1. Then the total cards: one with integer 1 and one blank? Actually n=1, there is one card with integer 1 and one blank, total 2 cards. But k is between 1 and n, so k ≤ n=1, so k=1 only. So we have one card, either 1 or 0. If it's 1, present[1]=true, blank=0, then longest interval with no missing: [1,1] length1. If it's 0, blank=1, present none, then sliding window: array length1 false, blank=1 -> interval length1 missing=1 ≤1 => output1. So both output1.
Thus sliding window works.
Now we need to be careful about the fact that the array size is n, which may be large but manageable.
Alternative approach: since k ≤ n, we can also solve using sorted list of present numbers and consider gaps. For blank=0, answer is longest consecutive subsequence within present numbers. For blank=1, answer is longest segment where there is at most one gap (i.e., the distance between two present numbers minus one is the number of missing numbers). But simpler to do sliding window on boolean array.
But note: n up to 100k, we can allocate boolean array of that size. That's fine.
Potential memory: For each dataset, we can allocate vector or bitset; but better to use vector or vector for speed.
Implementation steps:
- While true: read n, k.
- If n==0 && k==0: break.
- Initialize present array of size n+1 (index 1..n) with false.
- blank = 0.
- For i in 0..k-1: read x. If x==0: blank++. else present[x]=true.
- Perform sliding window:
left = 1
missing = 0
max_len = 0
for right in 1..n:
if not present[right]: missing++
while missing > blank:
if not present[left]: missing--
left++
max_len = max(max_len, right-left+1)
- Output max_len.
But careful: The blank count may be >1? But at most one. However, if input erroneously has multiple zeros, blank could be >1. The problem specification says only one blank card exists; but we can still handle blank >1 by treating it as ability to cover up to blank missing numbers. The sliding window would work for any blank count (though blank can be 0 or 1 only logically). So we can keep as is.
But we need to consider that the cards are given: we have exactly k cards; if there are multiple blanks, the total present count would be less than k - blank? Actually if there are multiple zeros, that would imply more than one blank card, which is impossible in the original set. But we can just accept.
Now, is there any other nuance? Let's re-read the problem statement carefully. It says:
"There are n cards with one integer from 1 to n and one blank card. Of these n + 1 cards, k cards are given, but 1 ≤ k ≤ n. You can write one integer from 1 to n on a blank card. I want to make a continuous sequence of integers as long as possible with just a given card."
The phrasing "with just a given card." might be "with just the given cards" meaning using only the given cards. So that's what we did.
The phrase "continuous sequence of integers" likely means a sequence of consecutive integers, like 4,5,6,7. Yes.
Thus solution stands.
But before finalizing, let's think about alternative interpretations. Could it be that we need to use all given cards? The phrase "with just a given card" might mean using only the given card(s) as a set? Usually not. "just a given card" might be a translation issue. It says: "I want to make a continuous sequence of integers as long as possible with just a given card." Possibly they meant "with the given cards" (the cards we have). So we are free to choose a subset.
Check second sample: They had cards: 6,2,0,4,7. They achieved 4 by using 4,5,6,7? Actually they used 4 (given), 6 (given), 7 (given), and blank as 5. That uses 4 cards out of 5 (they didn't use the 2). So they used a subset. So subset is allowed.
Thus our interpretation is correct.
Now, one more nuance: The blank card can be used to write any integer, but we could also choose not to write on it? But then it's blank and presumably cannot represent a number. So we must write on it to use it. So to use it, we have to assign a number. So the wildcard is optional; we can either use it or not. In our condition missing ≤ blank, we treat blank as capacity to cover missing numbers. If we don't use it, missing could be 0, which is fine. So condition is okay.
Thus solution is sliding window.
But wait: what about intervals that start and end at numbers that are not present? That's allowed, as long as missing count ≤ blank. So sliding window will consider all intervals.
Now, we need to ensure we handle the case where n can be up to 100,000; our sliding window passes over n, O(n). That's fine.
But is there a more efficient method when k is much smaller than n? For example, n=100,000, k=500. Then present array is mostly false. Sliding window over 100k is still fine; 100k is small. So it's fine.
Now, consider reading input: each dataset: first line n,k. Then k lines each with an integer. That's straightforward.
But there is a twist: The problem statement includes a note: "Of the scoring data, 40% of the points are 1 ≤ n ≤ 1000, 1 ≤ k ≤ 500, 20% of the points are 1 ≤ n ≤ 60000, 1 ≤ k ≤ 50000. Satisfy 1 ≤ n ≤ 100000, 1 ≤ k ≤ 100000." So our O(n) solution works for full score.
Thus implementation is trivial.
But wait, maybe there is a nuance about the blank card: It can be written on, but after writing, we cannot change it. So if we have a blank, we can fill it with exactly one integer. In the sliding window, we assumed we can fill exactly one missing number. But what if there are intervals that have exactly one missing number, but that missing number is, say, x, but we also need to use the blank for some other reason? Actually no other reason.
Thus sliding window is correct.
However, consider the scenario where the interval length L is exactly equal to the number of present numbers inside it + 1 (since one missing). But what if the present numbers inside the interval are not exactly the ones we have? That is automatically true. But what if we have more than one blank? Not possible.
Thus answer is simply the longest interval with at most blank missing numbers.
Now, but wait: The problem says "You can write one integer from 1 to n on a blank card." That means you can write exactly one integer; you cannot write multiple. So yes.
Thus solution is straightforward.
But before finalizing, let's double-check the second sample: Output 4. Our sliding window yields: present[1..7] = {1? no: 2,4,6,7 are present; 1,3,5 are false. blank=1. Let's simulate sliding window quickly to confirm we get 4.
We'll compute max length with at most 1 false.
Start: left=1, right moves.
I'll do a mental pass:
Initialize left=1, missing=0, max=0.
right=1: present[1]=false => missing=1. missing<=1? yes, so length=1, max=1.
right=2: present[2]=true => missing=1 (still). length=2, max=2.
right=3: present[3]=false => missing=2 >1, so need to move left until missing<=1.
while missing>1: left=1, present[1]=false => missing becomes 1, left=2.
Now length = right-left+1 = 3-2+1=2, max stays 2.
right=4: present[4]=true => missing=1, length=4-2+1=3, max=3.
right=5: present[5]=false => missing=2 >1 -> while: left=2, present[2]=true => missing stays 2? Actually if present[2]=true, then removing true doesn't change missing. So we need to keep moving: left=3, present[3]=false => missing becomes 1, left=4. Now length=5-4+1=2, max=3.
right=6: present[6]=true => missing=1, length=6-4+1=3, max=3.
right=7: present[7]=true => missing=1, length=7-4+1=4, max=4.
So max=4. Good.
Thus solution works.
Now, consider if blank=0, we need longest run of true. The sliding window with missing>0 will shrink until missing=0, so effectively finds longest window with all true. Works.
Now, what about the requirement "just a given card" - maybe they mean we must use all given cards? Let's read the original phrasing: "I want to make a continuous sequence of integers as long as possible with just a given card." Could be translation of "using only the given cards". In many problems, "given card" might be mis-translation of "given cards". So we are fine.
But let's look at the input: they have two datasets separated by a line with "0 0". In the sample, they gave:
7 5
6
2
4
7
1
7 5
6
2
0
4
7
0 0
Note that after first dataset (with 5 numbers: 6,2,4,7,1) there is a second dataset line "7 5". So they didn't have a blank line; it's continuous. So we need to parse accordingly.
Now, output is 2 and 4. So matches.
Thus we can code.
But perhaps there is a twist: The blank card can be used only once, but we might have the option to not use it. So b is either 0 or 1. But what if we have a blank and also we have a number that is missing, but we might have multiple intervals? No.
Thus solution is final.
Let's check some more tricky cases:
Case: n=10, k=5, cards: 1,2,3,4,0. present:1-4 true, blank=1. Then the longest interval with at most 1 missing: we can get [1,5] length5 (missing5), [1,6] length6 missing5,6 => missing2 >1. So max is 5? But could we get [1,10]? missing too many. Could we get [5,9]? present:6,7,8,9? Actually present only 1-4, so interval starting at 5 has many missing. But we can also consider [2,6]? missing3,5? Actually present 2,3,4; missing5,6? Wait [2,6] includes 2,3,4,5,6: present 2,3,4 (3 numbers) missing5,6 (2 missing) -> not allowed. So max is maybe [1,5] =5. However, we have total cards =5. We need to use 5 cards: we have 1,2,3,4 and blank. For [1,5], we need numbers 1,2,3,4,5. We have cards for 1,2,3,4 and blank can be 5. So uses all 5 cards. Good. So answer 5. Sliding window gives length 5? Let's simulate: present[1..10] true for 1-4. blank=1. Window:
right=1..4: max len=4 eventually? Let's do quickly: left=1. right=1: missing0? present1 true missing0 => len1, max1.
right=2: present true, missing0, len2, max2.
right=3: len3, max3.
right=4: len4, max4.
right=5: present5 false, missing=1, len5, max5.
right=6: false, missing=2 >1, shrink left. When left moves, we might lose some. At right=6, missing=2, need to shrink until missing<=1. Initially left=1: if left=1, present1 true -> missing stays2, left=2; present2 true -> missing2, left=3; present3 true -> missing2, left=4; present4 true -> missing2, left=5; present5 false -> missing becomes1, left=6. Then len=6-6+1=1. So max remains5. right=7: false, missing becomes2, shrink similarly, max unchanged. So max=5. So works.
Now, consider scenario where blank=1 but we have many present numbers that are scattered, and the longest window might involve using the blank to fill a gap, but there might be windows that include more than one gap but the blank can only fill one, so not allowed. So our condition covers.
Now, consider scenario where we have a blank and we have present numbers that include numbers outside the window; that doesn't affect.
Thus algorithm is correct.
Now, we need to implement reading of multiple datasets. The number of datasets ≤5. The input ends with "0 0".
Implementation in C++:
- Use scanf/printf for speed.
- For each dataset:
read n, k.
if n==0 && k==0 break.
vector present(n+1, false); // or vector.
int blank = 0;
for(int i=0; i<k; ++i) {
int x; scanf("%d", &x);
if(x==0) blank++;
else present[x] = true;
}
// sliding window
int left = 1;
int missing = 0;
int maxlen = 0;
for(int right=1; right<=n; ++right) {
if(!present[right]) missing++;
while(missing > blank) {
if(!present[left]) missing--;
left++;
}
maxlen = max(maxlen, right-left+1);
}
printf("%d\n", maxlen);
But note: present vector of size n+1; for n up to 100k, that's fine.
But I recall that vector is a bitset; it's fine but might be slower or have quirks. We can use vector for simplicity. But it's okay.
Potential issues: The input might have extra spaces or newlines; but using scanf should be fine.
Now, consider scenario where blank > 1, but that could be > 1 due to multiple zeros. Then our sliding window would allow missing up to blank, which would be > 1. However, is it logically correct? If we have multiple blanks, we could fill multiple missing numbers. But the problem only has one blank. So not needed. But just in case, we can keep as is.
But we need to consider the total cards count: if we have multiple blanks, total present count would be k - blank, and we might have more than one blank, then missing can be up to blank. But also we must ensure we have enough blanks to cover missing numbers. However, we also need to have enough total cards to cover the interval length. For interval length L, we need L - (number of present in interval) = missing numbers to be covered by blanks. So if blank >= missing, then feasible. So sliding window condition is still correct. However, the total number of blanks we have is blank, and we are using at most missing of them. So yes.
But is there any other constraint about using each blank only once? Each blank is a separate card that can be written with a number. So if we have multiple blanks, we can fill each with a different missing number. So indeed missing ≤ blank is sufficient.
But the original problem only has one blank, so we won't have multiple. But we can safely handle.
Edge Cases: If blank = k, meaning all cards are blanks? But k ≤ n and n≥1, but there is only one blank originally, so impossible. But we might ignore.
Thus solution is ready.
Now, could there be a more optimized approach if n is up to 10^9? But it's not; it's up to 100k.
Thus solution is final.
Let's also test with a scenario: n=100000, k=100000, all numbers present (no blank). Then present all true, blank=0, sliding window will get maxlen = n = 100000. That's fine.
If k=n and we have blank? But k≤n, and there is only one blank; if k=n, then we must be given n cards out of n+1. So it's possible that we are given all n number cards and not the blank, or we could be given n-1 number cards and the blank. That's allowed. So fine.
Now, potential pitfalls: If we use a boolean array, we need to reset it for each dataset. Since n varies, we can allocate fresh each time.
Time complexity: O(n) per dataset, plus reading k numbers O(k). Total O(n+k) ≤ 2*100k per dataset.
Thus acceptable.
Now, I think we have solved.
But before finalizing, let's double-check problem statement: "You can write one integer from 1 to n on a blank card. I want to make a continuous sequence of integers as long as possible with just a given card."
Could it be that we must use all given cards? The phrase "with just a given card" could be misinterpreted. However, sample 2 used only 4 out of 5 cards, discarding the 2. So subset allowed.
Thus correct.
We'll implement.
Now, consider alternative approach: sort the present numbers and find maximum gap. For b=0, answer is longest consecutive run. For b=1, answer is max over combining two consecutive runs separated by one gap (i.e., sum of lengths of two runs if gap is 1) or a run plus one if there is an extra number at boundary? Actually you could also extend a run by 1 if there is a missing number at one end that you can fill with blank, provided there is a card for that number? But we need to consider ability to fill missing number inside or at ends. Actually the sliding window is more straightforward.
But I'd like to confirm that the sliding window works for blank=0 and blank=1, and for cases where the best window might start at a missing number and end at a missing number but with only one missing inside? Actually if we have blank=1, the window may start and end at false positions, but as long as total missing count ≤ 1, that's fine. However, if the window starts with a false, that false is counted as missing. That's okay if we cover it with blank. But then the window also includes that false number. So we need a card for that number. That's the blank assigned to that number. So it's fine. Similarly, window can end at a false.
Thus sliding window works.
Now, think about the scenario where the best window includes two false numbers but they are the same? No.
Thus done.
Implementation details:
We'll use C++.
Potential issues: Input reading after the last dataset: after reading n,k=0,0 we break. But there might be trailing whitespace. fine.
Now, I should write code.
But wait: There is mention in problem statement: "The number of datasets does not exceed 5." So we can just loop.
Now, final code:
#include
#include
#include
using namespace std;
int main() {
int n, k;
while(scanf("%d %d", &n, &k) == 2) {
if(n == 0 && k == 0) break;
vector present(n+1, false);
int blank = 0;
for(int i=0; i<k; i++) {
int x; scanf("%d", &x);
if(x == 0) blank++;
else present[x] = true;
}
int left = 1;
int missing = 0;
int maxlen = 0;
for(int right = 1; right <= n; right++) {
if(!present[right]) missing++;
while(missing > blank) {
if(!present[left]) missing--;
left++;
}
maxlen = max(maxlen, right - left + 1);
}
printf("%d\n", maxlen);
}
return 0;
}
But note: vector may have performance issues with bit manipulation, but n=100k is okay. To be safe, we can use vector or bitset. However, vector might cause issues with references. I'll use vector for simplicity.
I'll change:
vector present(n+1, 0);
...
present[x] = 1;
And check: if(!present[right]) -> if(present[right]==0). Good.
Now, the missing count and sliding window.
But wait: In the sliding window, we use left pointer starting at 1, and we move left as we shrink. This is O(n) total because left moves at most n times.
But consider when blank = 0 and there are many false, the while loop could run many times for each right? Actually each element is removed at most once, so total O(n). So fine.
Thus solution.
Now, let's test on given sample.
First dataset:
n=7,k=5, numbers:6,2,4,7,1. Blank=0.
present:1,2,4,6,7 true.
Sliding window:
right=1: present[1] true -> missing0, length1, max1.
right=2: present[2] true -> length2, max2.
right=3: present[3] false -> missing1 >0 -> shrink left: left=1, present[1] true -> missing still1; left=2, present[2] true -> missing1; left=3, present[3] false -> missing0, left=4. length = 3-4+1=0? Actually right=3, left becomes 4, so window empty? Need to recalc: while missing>blank, we shrink. After left moves to 4, missing=0, loop ends. Then maxlen = max(prev, 3-4+1=0) -> remains2. That's fine.
right=4: present[4] true -> missing0, length=4-4+1=1, max2.
right=5: present[5] false -> missing1, shrink: left=4, present[4] true -> missing1, left=5, present[5] false -> missing0, left=6. length=5-6+1=0.
right=6: present[6] true -> missing0, left=6, length=1, max2.
right=7: present[7] true -> missing0, length=2, max2? Actually left=6, right=7, length=2, max2 remains2. So output2. Good.
Second dataset:
n=7,k=5, numbers:6,2,0,4,7. Blank=1.
present:2,4,6,7 true.
Sliding window as earlier gave max4. So output4. Good.
Thus correct.
Now, also test with scenario: n=1,k=1,x=0 => present none, blank=1. Window: right=1, present[1] false missing=1 <=1, length=1, max=1. Output1.
Works.
Now, consider dataset where there are multiple zeros: suppose n=5,k=3, numbers 0,0,1. But that cannot happen because only one blank exists, but if input gave it, blank=2, present[1]=true. Then sliding window with blank=2 would allow up to 2 missing. Let's see if answer would be something. But not needed.
Now, maybe we need to consider that the blank can only be used if it is present. That we accounted.
Thus final solution.
But before concluding, I want to double-check the problem statement for any hidden details: "Of these n + 1 cards, k cards are given, but 1 ≤ k ≤ n. You can write one integer from 1 to n on a blank card. I want to make a continuous sequence of integers as long as possible with just a given card."
The phrase "with just a given card" might be ambiguous: could it be that we have to make a continuous sequence using exactly the given cards (i.e., we must use all given cards)? But then in sample 2, if we must use all given cards, we have cards: 6,2,0,4,7. We need to form a contiguous sequence using all these cards. We could fill blank with, say, 5, then we have numbers 2,4,5,6,7. That set is {2,4,5,6,7}. Is that a continuous sequence? No, because it lacks 3. So not contiguous. Could we fill blank with 3? Then we have {2,3,4,6,7}. Not contiguous. Could we fill with 1? {1,2,4,6,7} not contiguous. Could we fill with 8? {2,4,6,7,8} not contiguous. So it's impossible to have all five numbers forming a contiguous block. So the maximum contiguous sequence using all cards would be maybe of length 2? For example using subset? But if we must use all, we can't. So the answer would not be 4. So clearly we are allowed to choose a subset. So our interpretation stands.
Thus final.
Now, about the solution: O(n) per dataset, fine.
But we should note: n can be up to 100,000 and we might need to read up to 100,000 numbers per dataset. Use fast IO.
Alright.
Now, I'm going to provide the final answer.
But hold on: The problem is from some contest; maybe there is a trick: The blank card can be written after we see the cards; we can choose any integer; but we are limited to only one blank. So yes.
One more nuance: The sequence must be "continuous sequence of integers", meaning that we need to have cards representing those integers, but they don't have to be in order? Usually continuous sequence just means a set of consecutive integers, not that we need to arrange them in order physically. So fine.
Thus solution stands.
Now, let's consider alternative edge case: Suppose we have present numbers that are many, but there is a long gap of missing numbers. With blank=1, we can only bridge one missing number. So the longest interval may be on one side of the gap. Sliding window will find it.
Now, what about when blank=1 and there is a missing number at the very beginning or end? For example, n=10, present numbers: 2,3,4,5,6,7,8,9,10 (missing 1). Blank=1. Then we can fill 1, so we get interval [1,10] length 10. Our sliding window will achieve that because missing count for [1,10] includes 1 missing (since 1 is false). missing=1 ≤1, length=10. So maxlen=10. But note: we have total cards = 9 present + 1 blank = 10 cards, exactly enough. So works.
Now, consider present: 1,2,3,4,5,6,7,8,9 (missing 10). Blank=1. Then [1,10] missing=1, length 10. Works.
Now, consider present: 1,3,5,7,9; blank=1. Then the longest interval? Could we get length 3? [1,3] missing2? present1,3, blank->2 => length3. But what about [5,7] length3 similar. Could we get length 4? [1,4] missing2,4? Actually [1,4] includes 1,2,3,4: present1,3 missing2,4 -> missing2 >1. So no. [3,6] 3,4,5,6 present3,5 missing4,6 -> 2 missing. So max3. Sliding window should find 3. Good.
Now, consider present: 1,2,4,5,6,7,9,10; blank=1. What is max? Possibly [4,10]? Let's see [4,10] numbers 4-10: present4,5,6,7,9,10 missing8? Actually missing8 only? Wait also missing? 4,5,6,7,8,9,10: present4,5,6,7,9,10 missing8 => one missing, length 7. Could there be [1,7]? 1-7: present1,2,4,5,6,7 missing3 => one missing, length 7 also. So max 7. Sliding window would catch.
Now, consider if we have blank=1 and the best window uses the blank to fill a gap, but the window also includes the blank's number as a present number? Not possible because that would be duplicate but doesn't hurt.
Now, maybe there is a twist: The blank card can be written on only once, but we can also decide not to use it. So sliding window condition missing ≤ blank covers.
Thus solution ready.
Now, regarding input: The sample input shows two datasets, but they are separated by a line containing "7 5". It is not preceded by blank line. So we just read as described.
Now, note: The problem statement says "The following k line contains one integer. Written one by one, representing the integers written on the given k cards." So after reading n,k, we read k integers, each on its own line. Then next dataset begins with n,k. So after finishing reading k integers, we read next line as n,k. So we need to be careful to not read extra.
Now, implement.
Potential pitfalls: For large n, the present array initialization O(n) each dataset is fine. Could also use vector and fill but O(n) anyway.
Now, ensure that the while loop condition is correct: while (missing > blank) shrink.
Now, we must output maxlen.
Now, final answer.
But before finalizing, I'd like to think if there is any scenario where the blank card cannot be used to fill a missing number because that number might already be covered by another card? Not an issue.
Also, what about the possibility that the blank card is used to replace a missing number, but we also need a card for that number: we have the blank, so that's fine.
Thus we can.
Now, could the sliding window approach miss intervals that start at 0? No.
Now, I'm going to write a more efficient version using two pointers with while.
Edge: If blank=0, the sliding window essentially finds longest subarray of consecutive true values. That is equivalent to scanning for runs of true.
Now, we must consider that the array indices are from 1 to n inclusive.
Now, code.
We'll include <bits/stdc++.h> or specific headers.
Now, consider alternative: Use bitset for memory but not necessary.
Now, final answer.
But wait, there is a note: The input size: k up to 100,000, n up to 100,000. So reading each integer with scanf is fine.
Now, time to produce final answer.
But the problem statement includes "The number of datasets does not exceed 5." So our loop works.
Now, let's write final solution in C++.
Also note: The sample input ends with "0 0". So we need to break.
I'll produce final code.
But before that, maybe we can improve by using a prefix sum of missing counts? But sliding window is simpler.
Thus final answer.
Now, write final explanation.
But the problem asks: "Write a program that outputs the maximum length of a contiguous sequence of integers that can be made from a given card when the given card is entered." Possibly "given card" might mean "given cards". So we're good.
Now, I'll produce the final solution. ```cpp
#include
#include
#include
using namespace std;
int main() {
int n, k;
while (scanf("%d %d", &n, &k) == 2) {
if (n == 0 && k == 0) break;
// present[i] == 1 if we have the card with number i, else 0
vector<char> present(n + 1, 0);
int blank = 0;
for (int i = 0; i < k; ++i) {
int x;
scanf("%d", &x);
if (x == 0) ++blank;
else present[x] = 1;
}
// sliding window
int left = 1, missing = 0, maxlen = 0;
for (int right = 1; right <= n; ++right) {
if (present[right] == 0) ++missing;
while (missing > blank) {
if (present[left] == 0) --missing;
++left;
}
maxlen = max(maxlen, right - left + 1);
}
printf("%d\n", maxlen);
}
return 0;
}