Let $ n $ be the total number of apartments, numbered $ 1 $ to $ n $, arranged in a row. Let $ k $ be the number of already inhabited apartments ($ 0 \leq k \leq n $).
An apartment is *good* if:
- It is **available** for sale (i.e., not inhabited),
- And it has **at least one adjacent** apartment that is inhabited.
---
### Definitions:
Let $ H \subseteq \{1, 2, \dots, n\} $ be the set of inhabited apartments, with $ |H| = k $.
Let $ G(H) $ be the set of good apartments given $ H $.
We seek:
- $ \min_{|H| = k} |G(H)| $
- $ \max_{|H| = k} |G(H)| $
---
### Constraints:
- Apartments at positions $ 1 $ and $ n $ have only one neighbor each.
- Apartments $ 2 $ to $ n-1 $ have two neighbors.
- A good apartment must be **uninhabited** and adjacent to **at least one** inhabited apartment.
---
## Minimum Number of Good Apartments
To **minimize** the number of good apartments, cluster the $ k $ inhabited apartments together in a single contiguous block.
- If $ k = 0 $: No inhabited apartments → 0 good apartments.
- If $ k = n $: No available apartments → 0 good apartments.
- Otherwise, place all $ k $ inhabited apartments consecutively, say from position $ a $ to $ a+k-1 $.
Then, the only available apartments adjacent to inhabited ones are:
- The one immediately before the block (if $ a > 1 $),
- The one immediately after the block (if $ a+k-1 < n $).
Thus, maximum 2 good apartments.
But if the block touches an end (e.g., starts at 1 or ends at $ n $), then only one side is adjacent.
So:
- If the block is placed at one end (e.g., apartments $ 1 $ to $ k $), then only apartment $ k+1 $ is good (if $ k < n $).
- Similarly, if placed at the other end, only $ n-k $ is good.
Hence, **minimum** number of good apartments is:
$$
\min_{|H|=k} |G(H)| =
\begin{cases}
0 & \text{if } k = 0 \text{ or } k = n \\
1 & \text{if } 1 \leq k \leq n-1 \text{ and the block is placed at an end} \\
\end{cases}
$$
But even if we place the block in the middle, we get 2 good apartments. To **minimize**, we place it at the end → only 1 good apartment.
**However**, if $ k = 1 $ and $ n = 1 $, then $ k = n $ → 0.
So general formula:
- If $ k = 0 $ or $ k = n $: **0**
- Else: **1** (achieved by placing the block at one end)
> **But wait**: What if $ k = 1 $ and $ n = 2 $? Then the single inhabited apartment has one neighbor — which is available → 1 good apartment. Still 1.
> What if $ k = 2 $, $ n = 3 $? Inhabited: [1,2] → good: [3]. Or [2,3] → good: [1]. Still 1.
> What if $ k = 2 $, $ n = 4 $? Inhabited: [1,2] → good: [3]. Only one.
So yes — **minimum is always 0 if k=0 or k=n, else 1**.
Wait — what if $ k = n-1 $? Then only one apartment is available. It must be adjacent to at least one inhabited one (since only one is missing, and the rest are filled). So the single available apartment is adjacent to at least one inhabited → 1 good apartment.
So **minimum is always 1 when $ 1 \leq k \leq n-1 $**.
✅ **Minimum = $ \max(0, \min(2, k)) $**? No.
Actually: **Minimum = 0 if k=0 or k=n, else 1**
---
## Maximum Number of Good Apartments
To **maximize** the number of good apartments, we want to **maximize the number of available apartments that are adjacent to at least one inhabited apartment**.
Each inhabited apartment can "cover" up to 2 adjacent available apartments (left and right), **but**:
- If two inhabited apartments are adjacent, the space between them is not available → no gain.
- If they are separated by at least one available apartment, then each can cover their own neighbors.
To maximize coverage, **space out the inhabited apartments** so that no two are adjacent.
That is, place inhabited apartments with **at least one gap** between them.
In this case, each inhabited apartment can contribute **two** good apartments — unless it is at the boundary.
But if we place them with exactly one gap between them (i.e., pattern: I - I - I - ...), then:
- Each inhabited apartment (except possibly at ends) has two neighbors, both available → contributes 2 good apartments.
- But the available apartments between them are shared? No — if we have:
Positions: I - I - I
Then apartments: 1 (I), 2 (available), 3 (I), 4 (available), 5 (I)
Good apartments: 2, 4 → only 2 good apartments for 3 inhabited.
Wait — apartment 2 is adjacent to 1 and 3 → both inhabited → still only counted once.
Apartment 4 is adjacent to 3 and 5 → both inhabited → still one apartment.
So each **gap** between two inhabited apartments (i.e., a single available apartment between two inhabited) becomes **one good apartment**.
Also, if an inhabited apartment is at the end, it can make its single neighbor good.
So if we place $ k $ inhabited apartments with **at least one gap** between each pair, then:
- Number of gaps between inhabited apartments: $ k - 1 $
- Each such gap is a single available apartment between two inhabited → contributes 1 good apartment.
- Additionally, if the first inhabited is not at position 1, then position 1 is available and adjacent to the first inhabited → good.
- Similarly, if the last inhabited is not at position $ n $, then position $ n $ is good.
So total good apartments =
- $ k - 1 $ (gaps between inhabited)
- + 1 if the first inhabited is not at 1
- + 1 if the last inhabited is not at n
To **maximize**, we want to **maximize the number of available apartments adjacent to inhabited ones**, so we should **minimize overlap** of coverage.
Best strategy: **place inhabited apartments as isolated as possible**, i.e., with **exactly one available apartment between them**, and **not touching the ends** if possible.
But we can also choose to **place them at the ends** to get extra coverage.
Actually, the optimal configuration is to **space them with single gaps**, and **extend as far as possible**.
Let’s think differently.
Each inhabited apartment can "cover" up to 2 available neighbors.
But if two inhabited apartments are adjacent, they share a boundary and cover fewer total available apartments.
To maximize total good apartments, we want **no two inhabited apartments adjacent**, and we want to **avoid placing them at the ends**? No — actually, placing at ends gives **one** good neighbor each, same as internal ones give two.
Wait — if an inhabited apartment is at position 1, it can only cover position 2.
If it’s at position 2, it can cover 1 and 3.
So **internal** inhabited apartments can cover **two** good apartments; **end** ones cover **one**.
So to maximize, we should **avoid placing inhabited apartments at the ends**, so that each can cover two neighbors.
But we have only $ k $ inhabited apartments.
Suppose we place all $ k $ inhabited apartments in the interior, each separated by at least one available apartment.
Then, each inhabited apartment contributes 2 good apartments → total $ 2k $.
But can we do that?
We need to place $ k $ inhabited apartments with **at least one gap** between them.
Minimum space required: each inhabited apartment takes 1 spot, and between each pair, we need 1 gap → total length = $ k + (k - 1) = 2k - 1 $
So if $ 2k - 1 \leq n $, we can place them all internally with gaps → each contributes 2 good apartments → total good apartments = $ 2k $
But if $ 2k - 1 > n $, then we cannot space them all with gaps → some must be adjacent.
But also, if we place them at the ends, we get less coverage.
Wait — here's a better way.
In the optimal configuration for **maximum** good apartments:
- Place inhabited apartments with **exactly one available apartment between each pair**.
- Place the first inhabited apartment at position 2, then 4, 6, ..., so that they are not at the ends.
Then:
- Each inhabited apartment is surrounded by two available apartments → contributes 2 good apartments.
- The available apartments at the ends (1 and n) are not adjacent to any inhabited → not good.
But if we shift the entire block to the left, putting the first inhabited at position 1, then we lose one good apartment (no left neighbor), but we might be able to fit more?
Actually, the maximum number of good apartments is bounded by the number of available apartments that are adjacent to at least one inhabited.
Each inhabited apartment can be adjacent to at most 2 available apartments.
But if two inhabited apartments are adjacent, they share a boundary — the apartment between them is inhabited, so no good apartment there.
So the **maximum** number of good apartments is achieved when **no two inhabited apartments are adjacent**, and **we use as many available apartments as possible as neighbors**.
In that case, each inhabited apartment contributes **2** good apartments, **except** if it is at the end.
So to maximize, we want **no inhabited apartment at the ends** → then all $ k $ inhabited apartments are internal and each contributes 2 → total $ 2k $
But we can only do that if the total length required is $ \leq n $
To place $ k $ non-adjacent inhabited apartments, we need at least $ k $ positions for them and $ k-1 $ gaps between them → total $ 2k - 1 $
So if $ 2k - 1 \leq n $, then we can place them all internally (e.g., positions 2, 4, ..., 2k) → then the good apartments are positions 1, 3, 5, ..., 2k+1 — but wait, position 1 is adjacent to 2 → good, position 3 is adjacent to 2 and 4 → good, etc.
Actually, if we place inhabited at positions $ 2, 4, 6, \dots, 2k $, then:
- Good apartments: 1 (adj to 2), 3 (adj to 2 and 4), 5 (adj to 4 and 6), ..., $ 2k - 1 $, and $ 2k + 1 $ (if $ 2k + 1 \leq n $)
But we only have $ k $ inhabited apartments → the good ones are:
- Left of first: 1
- Between each pair: 3,5,..., up to $ 2k-1 $ → $ k-1 $ of them
- Right of last: $ 2k+1 $
So total good apartments = $ 1 + (k - 1) + 1 = k + 1 $
Wait — this contradicts.
Alternative approach:
Let’s model the maximum number of good apartments as the number of available apartments that are adjacent to at least one inhabited.
Let $ A $ be the set of available apartments.
Each inhabited apartment can "cover" its left and right neighbors, if they are available.
The total number of "adjacent pairs" between inhabited and available is at most $ 2k $, but each good apartment is counted once, even if adjacent to two inhabited.
So the maximum number of good apartments is **min(n - k, 2k)**
Why?
- There are $ n - k $ available apartments → can't have more than that many good apartments.
- Each inhabited apartment can contribute to at most 2 good apartments → total coverage capacity is $ 2k $
- So total good apartments ≤ $ \min(n - k, 2k) $
And this bound is **achievable**:
- If $ 2k \leq n - k $ → $ 3k \leq n $, then we can place $ k $ inhabited apartments, each separated by at least two available apartments → each inhabited apartment has two available neighbors, and no overlap → total good = $ 2k $
- If $ 2k > n - k $ → $ 3k > n $, then we have more coverage capacity than available apartments → we can make **all** available apartments good → total = $ n - k $
So:
✅ **Maximum = $ \min(n - k, 2k) $**
But wait — what if $ k = 0 $? Then $ \min(n, 0) = 0 $ → correct.
What if $ k = n $? Then $ \min(0, 2n) = 0 $ → correct.
What if $ n = 6, k = 3 $: $ \min(3, 6) = 3 $
Can we get 3 good apartments? Yes — e.g., inhabit 1,3,5 → good: 2,4,6 → 3 good → matches.
If $ n = 5, k = 3 $: $ \min(2, 6) = 2 $
Can we get 2 good? Inhabit 1,3,5 → available: 2,4 → both adjacent to inhabited → 2 good → yes.
If $ n = 4, k = 2 $: $ \min(2, 4) = 2 $
Inhabit 1 and 4 → good: 2 and 3 → 2 good → yes.
Inhabit 2 and 3 → adjacent → good: 1 and 4 → still 2.
So yes.
✅ So maximum = $ \min(n - k, 2k) $
---
## Final Answer:
$$
\boxed{
\begin{array}{c}
\text{Minimum} = \\
\begin{cases}
0 & \text{if } k = 0 \text{ or } k = n \\
1 & \text{otherwise}
\end{cases}
\\
\\
\text{Maximum} = \min(n - k, 2k)
\end{array}
}
$$