This problem shows up frequently in interviews because it tests whether we can track how information spreads across days under constraints like “people begin sharing only after delay days” and “they stop contributing once forget days have passed.” To solve it efficiently, we use Dynamic Programming, tracking how many people learn the secret each day and using a sliding window to model who can currently share.
Problem statement
You’re given three integers n, delay, and forget.
On day 1, exactly one person discovers a secret.
- After someone learns the secret, they will wait
delaydays, then start telling exactly one new person per day. - After
forgetdays, they forget the secret and stop sharing forever (they cannot share on the day they forget, or any day after).
Your task is to compute how many people still know the secret at the end of day n. As the answer may be very large, return the answer in modulo 109 + 7.
Constraints
- 2 <=
n<= 1000 - 1 <=
delay<forget<=n
Examples
| Input | Output | Explanation |
| n=5, delay=2, forget=4 | 3 | – Day 1: Person A learns the secret. – Day 3–4: A can share the secret (starts after 2 days). – Day 5: A forgets (learned on day 1, forget after 4 days → forget on day 5), so A is not counted. New people learned on day 3 and 4, both still remember on day 5 → total 2; plus the person learned on day 5 from the day 3 learner (who started sharing on day 5), so a total of 3 people know the secret. |
| n=4, delay=1, forget=3 | 6 | Everyone starts sharing the very next day (delay = 1) and forgets exactly 3 days after learning (forget = 3). That means each person can share for exactly 2 days (the day after they learn, and the day after that). Because each active person teaches one new person per day, the number of new people grows quickly: – Day 1: 1 person knows the secret. – Day 2: 1 new person is told → total becomes 2. – Day 3: 2 people can share → 2 new people are told → total becomes 4. – Day 4: 4 people can share, but the day-1 person forgets today and is no longer counted → 3 new people are told → total becomes 6 who still remember by the end of the day. |
Why tracking individuals is impractical
A naive approach to solving this problem is to track each person, simulate the days they can share, and recursively create new people for each share. The population grows exponentially (branching process), so this becomes exponential time and completely infeasible even for n=1000. It also repeats the same “how many people exist on day d” subcomputations many times (classic overlapping subproblems).
Optimized approach using Dynamic Programming
Instead of tracking individuals, we group people by the day they learned the secret. Let dp[d] represent the number of people who discover the secret on day d.
- A cohort that learns on day
xbegins sharing on dayx + delay - This same cohort stops sharing on day
x + forget(when they forget) - Between these two boundaries, each person in the cohort shares with one new person per day
On any given day d, the total number of new learners equals the number of currently active sharers. To find this, we sum across all cohorts whose sharing window includes day d, specifically, cohorts from days d - forget + 1 through d - delay.
This transforms an exponential individual-tracking problem into a linear scan over days, where each day we:
- Calculate active sharers (the sum of recent cohorts still eligible to share)
- Record that count as the new cohort for day
d - Accumulate the count of people who still remember the secret
Step-by-step algorithm
- Initialize an array
dpof lengthn + 1with0. - Set
dp[1] = 1(one person learns on day 1). - Initialize
sharewith0. This tracks the number of people who can share on the current day. - For each
dayfrom2ton:- Compute
startDay = day - delay, which is the discovery day of the cohort that becomes eligible to start sharing on the current day. - If
startDayis greater than or equal to1,- Add
dp[startDay]tosharebecause the people who learned onstartDaybecome eligible to start sharing today.
- Add
- Initiliaze
forgetDayby computingday - forget. - If
forgetDayis greater than or equal to1,- Subtract
dp[forgetDay]fromsharebecause the people who learned onforget_dayforget the secret today, and can no longer share.
- Subtract
- Set
dp[day] = sharebecause each person who is eligible to share tells the secret to exactly one new person today, so the number of new learners equals the current number of active sharers.
- Compute
- Set
startto themaxof1andn - forget + 1, wheren - forget + 1is the earliest day whose learners still remember the secret on dayn, andmaxensuresstartnever falls below day1. - Set
ansto thesumofdp[day]fromstartthroughn(the slice ends atn + 1to include dayn), then apply% MOD, whereMOD = 10^9 + 7, to keep the result within the required modulus. - In the end, return
ans.
Let’s look at the following illustration to better understand the solution.
Code implementation
Time complexity
The time complexity of this algorithm is O(n) because we iterate once from day 2 to day n, doing O(1) work per day.
Space complexity
The space complexity of this algorithm is O(n) because we store the dp array of size n+1 to later sum the cohorts that still remember on day n.
Common pitfalls and interview tips
- Off-by-one on forgetting day: If someone forgets on day x + forget, they cannot share on that day, and they should not be counted as knowing at the end of that day.
- Negative modulo during subtraction: In Python,
share = (share - value) % MODis safe; in other languages, where%can return a negative value, useshare = (share - value + MOD) % MODto keep the running count non-negative. - Wrong final counting window: The final answer is not “active sharers.” It is everyone who learned within the last
forget - 1days, i.e., days[n - forget + 1, n].