Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer n and an array sick sorted in increasing order, representing positions of infected people in a line of n people.
At each step, one uninfected person adjacent to an infected person gets infected. This process continues until everyone is infected.
An infection sequence is the order in which uninfected people become infected, excluding those initially infected.
Return the number of different infection sequences possible, modulo 109+7.
Example 1:
Input: n = 5, sick = [0,4]
Output: 4
Explanation:
There is a total of 6 different sequences overall.
[1,2,3], [1,3,2], [3,2,1] and [3,1,2].[2,3,1] and [2,1,3] are not valid infection sequences because the person at index 2 cannot be infected at the first step.Example 2:
Input: n = 4, sick = [1]
Output: 3
Explanation:
There is a total of 6 different sequences overall.
[0,2,3], [2,0,3] and [2,3,0].[3,2,0], [3,0,2], and [0,3,2] are not valid infection sequences because the infection starts at the person at index 1, then the order of infection is 2, then 3, and hence 3 cannot be infected earlier than 2.Constraints:
2 <= n <= 1051 <= sick.length <= n - 10 <= sick[i] <= n - 1sick is sorted in increasing order.Problem summary: You are given an integer n and an array sick sorted in increasing order, representing positions of infected people in a line of n people. At each step, one uninfected person adjacent to an infected person gets infected. This process continues until everyone is infected. An infection sequence is the order in which uninfected people become infected, excluding those initially infected. Return the number of different infection sequences possible, modulo 109+7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
5 [0,4]
4 [1]
contain-virus)amount-of-time-for-binary-tree-to-be-infected)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2954: Count the Number of Infection Sequences
class Solution {
private static final int MOD = (int) (1e9 + 7);
private static final int MX = 100000;
private static final int[] FAC = new int[MX + 1];
static {
FAC[0] = 1;
for (int i = 1; i <= MX; i++) {
FAC[i] = (int) ((long) FAC[i - 1] * i % MOD);
}
}
public int numberOfSequence(int n, int[] sick) {
int m = sick.length;
int[] nums = new int[m + 1];
nums[0] = sick[0];
nums[m] = n - sick[m - 1] - 1;
for (int i = 1; i < m; i++) {
nums[i] = sick[i] - sick[i - 1] - 1;
}
int s = 0;
for (int x : nums) {
s += x;
}
int ans = FAC[s];
for (int x : nums) {
if (x > 0) {
ans = (int) ((long) ans * qpow(FAC[x], MOD - 2) % MOD);
}
}
for (int i = 1; i < nums.length - 1; ++i) {
if (nums[i] > 1) {
ans = (int) ((long) ans * qpow(2, nums[i] - 1) % MOD);
}
}
return ans;
}
private int qpow(long a, long n) {
long ans = 1;
for (; n > 0; n >>= 1) {
if ((n & 1) == 1) {
ans = ans * a % MOD;
}
a = a * a % MOD;
}
return (int) ans;
}
}
// Accepted solution for LeetCode #2954: Count the Number of Infection Sequences
const MX = 1e5
const MOD = 1e9 + 7
var fac [MX + 1]int
func init() {
fac[0] = 1
for i := 1; i <= MX; i++ {
fac[i] = fac[i-1] * i % MOD
}
}
func qpow(a, n int) int {
ans := 1
for n > 0 {
if n&1 == 1 {
ans = (ans * a) % MOD
}
a = (a * a) % MOD
n >>= 1
}
return ans
}
func numberOfSequence(n int, sick []int) int {
m := len(sick)
nums := make([]int, m+1)
nums[0] = sick[0]
nums[m] = n - sick[m-1] - 1
for i := 1; i < m; i++ {
nums[i] = sick[i] - sick[i-1] - 1
}
s := 0
for _, x := range nums {
s += x
}
ans := fac[s]
for _, x := range nums {
if x > 0 {
ans = ans * qpow(fac[x], MOD-2) % MOD
}
}
for i := 1; i < len(nums)-1; i++ {
if nums[i] > 1 {
ans = ans * qpow(2, nums[i]-1) % MOD
}
}
return ans
}
# Accepted solution for LeetCode #2954: Count the Number of Infection Sequences
mod = 10**9 + 7
mx = 10**5
fac = [1] * (mx + 1)
for i in range(2, mx + 1):
fac[i] = fac[i - 1] * i % mod
class Solution:
def numberOfSequence(self, n: int, sick: List[int]) -> int:
nums = [b - a - 1 for a, b in pairwise([-1] + sick + [n])]
ans = 1
s = sum(nums)
ans = fac[s]
for x in nums:
if x:
ans = ans * pow(fac[x], mod - 2, mod) % mod
for x in nums[1:-1]:
if x > 1:
ans = ans * pow(2, x - 1, mod) % mod
return ans
// Accepted solution for LeetCode #2954: Count the Number of Infection Sequences
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2954: Count the Number of Infection Sequences
// class Solution {
// private static final int MOD = (int) (1e9 + 7);
// private static final int MX = 100000;
// private static final int[] FAC = new int[MX + 1];
//
// static {
// FAC[0] = 1;
// for (int i = 1; i <= MX; i++) {
// FAC[i] = (int) ((long) FAC[i - 1] * i % MOD);
// }
// }
//
// public int numberOfSequence(int n, int[] sick) {
// int m = sick.length;
// int[] nums = new int[m + 1];
// nums[0] = sick[0];
// nums[m] = n - sick[m - 1] - 1;
// for (int i = 1; i < m; i++) {
// nums[i] = sick[i] - sick[i - 1] - 1;
// }
// int s = 0;
// for (int x : nums) {
// s += x;
// }
// int ans = FAC[s];
// for (int x : nums) {
// if (x > 0) {
// ans = (int) ((long) ans * qpow(FAC[x], MOD - 2) % MOD);
// }
// }
// for (int i = 1; i < nums.length - 1; ++i) {
// if (nums[i] > 1) {
// ans = (int) ((long) ans * qpow(2, nums[i] - 1) % MOD);
// }
// }
// return ans;
// }
//
// private int qpow(long a, long n) {
// long ans = 1;
// for (; n > 0; n >>= 1) {
// if ((n & 1) == 1) {
// ans = ans * a % MOD;
// }
// a = a * a % MOD;
// }
// return (int) ans;
// }
// }
// Accepted solution for LeetCode #2954: Count the Number of Infection Sequences
const MX = 1e5;
const MOD: bigint = BigInt(1e9 + 7);
const fac: bigint[] = Array(MX + 1);
const init = (() => {
fac[0] = 1n;
for (let i = 1; i <= MX; ++i) {
fac[i] = (fac[i - 1] * BigInt(i)) % MOD;
}
return 0;
})();
function qpow(a: bigint, n: number): bigint {
let ans = 1n;
for (; n > 0; n >>= 1) {
if (n & 1) {
ans = (ans * a) % MOD;
}
a = (a * a) % MOD;
}
return ans;
}
function numberOfSequence(n: number, sick: number[]): number {
const m = sick.length;
const nums: number[] = Array(m + 1);
nums[0] = sick[0];
nums[m] = n - sick[m - 1] - 1;
for (let i = 1; i < m; i++) {
nums[i] = sick[i] - sick[i - 1] - 1;
}
const s = nums.reduce((acc, x) => acc + x, 0);
let ans = fac[s];
for (let x of nums) {
if (x > 0) {
ans = (ans * qpow(fac[x], Number(MOD) - 2)) % MOD;
}
}
for (let i = 1; i < nums.length - 1; ++i) {
if (nums[i] > 1) {
ans = (ans * qpow(2n, nums[i] - 1)) % MOD;
}
}
return Number(ans);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.