AlgoMaster Logo

Number of People Aware of a Secret

mediumFrequencyUpdated September 21, 2026

Understanding the Problem

We are simulating the spread of a secret over n days. One person discovers the secret on day 1. After a delay period, they start sharing it with one new person each day. After forget days, they forget the secret entirely and stop sharing.

Every new person who learns the secret follows the same rules: they wait delay days before sharing, and they forget after forget days. This creates a cascading effect where each group of knowers spawns the next, while earlier groups eventually drop off.

The core mechanic is the interplay between the sharing window and the forgetting window. A person who discovers the secret on day d can share it from day d + delay through day d + forget - 1. On day d + forget, they have already forgotten. We need to count how many people still know the secret on day n.

The structure to exploit is that everyone who discovers the secret on the same day behaves identically. So instead of tracking individual people, we can track how many new people discover the secret on each day, then sum up everyone who discovered it recently enough to still remember.

Key Constraints:

  • 2 <= n <= 1000 --> The bound is small, so even an O(n^2) day-by-day sum (about 10^6 operations) runs comfortably. An O(n) solution exists and is the target here.
  • 1 <= delay < forget <= n --> Since delay < forget, every person has at least one day to share the secret before forgetting, so the spread never stalls.

Approach 1: Dynamic Programming (Day-by-Day DP)

Intuition

We do not need to track individual people. Define dp[i] as the number of people who discover the secret on day i. Everyone who discovers the secret on the same day has the same sharing and forgetting schedule, so grouping by discovery day avoids the redundancy of tracking each person separately.

A person who discovers the secret on day j will share it on days j + delay, j + delay + 1, ..., j + forget - 1. On each of those days, they create exactly one new person. So the number of new people on day i is the sum of dp[j] for all j where j + delay <= i and j + forget - 1 >= i (equivalently, i - forget + 1 <= j <= i - delay).

This gives us: dp[i] = sum of dp[j] for j in [i - forget + 1, i - delay].

At the end, the answer is the sum of dp[j] for all j where j + forget > n (people who haven't forgotten by day n).

Algorithm

  1. Create an array dp of size n + 1, initialized to 0.
  2. Set dp[1] = 1 (one person discovers the secret on day 1).
  3. For each day i from 2 to n:
    • Sum up dp[j] for all j from max(1, i - forget + 1) to i - delay.
    • Set dp[i] to this sum (modulo 10^9 + 7).
  4. Sum up dp[j] for all j where j + forget > n to get the final answer.
  5. Return this sum modulo 10^9 + 7.

Visualization and Code

Loading animation...

Adjacent days have overlapping summation ranges that differ by at most two elements. The next approach maintains a running sum of the sharing window and updates it incrementally instead of re-summing the window each day.

Approach 2: Optimized DP with Prefix Sum

Intuition

The inner loop in Approach 1 computes a range sum: dp[i] = sum of dp[j] for j in [i - forget + 1, i - delay]. Adjacent windows overlap heavily, so we can maintain the sum incrementally instead of recomputing it. Track a running share variable that holds how many people are actively sharing on the current day.

When we process day i, the sharing window shifts by one compared to day i - 1:

  • One new group enters the sharing window: the people from day i - delay (their delay period just ended, so they begin sharing on day i).
  • One group exits the sharing window: the people from day i - forget (they forget on day i and stop sharing).

So we update the running sum with share += dp[i - delay] (new sharers enter) and share -= dp[i - forget] (old sharers exit), then set dp[i] = share. This computes dp[i] in O(1) per day.

Algorithm

  1. Create an array dp of size n + 1, initialized to 0. Set dp[1] = 1.
  2. Maintain a variable share = 0 representing how many people are actively sharing today.
  3. For each day i from 2 to n:
    • If i - delay >= 1, add dp[i - delay] to share (people from day i - delay just entered their sharing window).
    • If i - forget >= 1, subtract dp[i - forget] from share (people from day i - forget just forgot).
    • Set dp[i] = share.
  4. Sum dp[j] for all j where j + forget > n to get the final answer.
  5. Return the answer modulo 10^9 + 7.

Visualization and Code

Loading animation...