Count the Number of Infection Sequences - Problem
Pandemic Spread Simulation

Imagine a pandemic spreading through a line of n people, where some individuals are initially infected. You're given an integer n representing the total number of people and an array sick (sorted in increasing order) representing the positions of people who are already infected.

The infection spreads with these rules:
• At each time step, exactly one uninfected person adjacent to an infected person becomes infected
• This continues until everyone is infected
• An infection sequence is the specific order in which the uninfected people become infected

Goal: Calculate how many different infection sequences are possible.

Example: If n=5 and sick=[0,4] (positions 0 and 4 are infected), the infection can spread inward from both ends. Person at position 1 or 3 could be infected first, leading to different sequences.

Return the answer modulo 109 + 7.

Input & Output

example_1.py — Basic Case
$ Input: n = 5, sick = [0, 4]
Output: 4
💡 Note: People at positions 0 and 4 are initially infected. The infection can spread inward. Possible sequences: [1,2,3], [1,3,2], [3,2,1], [3,1,2] - total of 4 different sequences.
example_2.py — Single Group
$ Input: n = 4, sick = [1]
Output: 6
💡 Note: Only position 1 is infected initially. Infection can spread left to 0 and right to 2,3. The sequences are different orderings of [0,2,3], giving us 3! = 6 possibilities.
example_3.py — All Infected
$ Input: n = 3, sick = [0, 1, 2]
Output: 1
💡 Note: Everyone is already infected, so there's only 1 way (no additional infections needed).

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ sick.length ≤ n
  • 0 ≤ sick[i] ≤ n - 1
  • sick is sorted in increasing order
  • All values in sick are distinct

Visualization

Tap to expand
Infection Sequence Counting StrategyStep 1: Identify Independent GroupsG1IG2G2IG3Size 1Size 2Size 1Step 2: Calculate Group PossibilitiesGroup 1 (Boundary):• Size: 1 person• Ways: C(total,1) = total• Only spreads inwardGroup 2 (Middle):• Size: 2 people• Ways: C(total,2) × 2¹• Spreads from both endsGroup 3 (Boundary):• Size: 1 person• Ways: C(remaining,1)• Only spreads inwardStep 3: Apply Multiplication PrincipleTotal Sequences = Group1 × Group2 × Group3Each group spreads independently, so multiply possibilitiesKey Mathematical Insight:💡 Combinatorial Formula:• Boundary groups: Choose when in sequence to infect (binomial coefficient)• Middle groups: Choose when + choose direction (binomial × 2^(size-1))• Total: Product of all group possibilities (multiplication principle)
Understanding the Visualization
1
Identify Groups
Split uninfected people into isolated groups between infected positions
2
Group Independence
Each group spreads independently - use multiplication principle
3
Calculate Coefficients
For each group, calculate how many ways it can be infected using combinatorics
4
Combine Results
Multiply all group possibilities to get total sequences
Key Takeaway
🎯 Key Insight: Transform the infection simulation problem into a combinatorial counting problem by recognizing that isolated groups of uninfected people spread independently, allowing us to use mathematical formulas instead of expensive simulation.
Asked in
Google 28 Meta 22 Amazon 15 Microsoft 12
26.1K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen