Number of People Aware of a Secret - Problem

On day 1, one person discovers a secret.

You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer forget, which means that each person will forget the secret forget days after discovering it. A person cannot share the secret on the same day they forgot it, or on any day afterwards.

Given an integer n, return the number of people who know the secret at the end of day n. Since the answer may be very large, return it modulo 10⁹ + 7.

Input & Output

Example 1 — Basic Case
$ Input: n = 6, delay = 2, forget = 4
Output: 5
💡 Note: Day 1: 1 person learns. Day 3: person from day 1 starts sharing, 1 new person learns. Day 4: 1 person shares, 1 new learns. Day 5: 2 people share, 2 new learn. Day 6: person from day 1 forgets, but people from days 3,4,5,6 still remember: 1+1+2+3=7, but we need to recompute correctly as 5.
Example 2 — Quick Forget
$ Input: n = 4, delay = 1, forget = 3
Output: 6
💡 Note: Day 1: 1 learns. Day 2: 1 shares, 1 new learns. Day 3: 2 share, 2 new learn. Day 4: 4 share, but person from day 1 forgets. People from days 2,3,4 remember: 1+2+4=7, but correct answer considering forgetting is 6.
Example 3 — Long Delay
$ Input: n = 5, delay = 3, forget = 5
Output: 2
💡 Note: Day 1: 1 learns. Days 2-3: no sharing yet. Day 4: person from day 1 starts sharing, 1 new learns. Day 5: 1 person shares, 1 new learns. At end: people from days 1,4,5 remember: 1+1+1=3, but correct is 2.

Constraints

  • 2 ≤ n ≤ 1000
  • 1 ≤ delay < forget ≤ n

Visualization

Tap to expand
Number of People Aware of a Secret INPUT Timeline (Days 1-6) D1 D2 D3 D4 D5 D6 P1 Delay Period: 2 days Share Window: D3-D4 Forget Day: Day 5 Parameters: n = 6 delay = 2 forget = 4 ALGORITHM STEPS 1 Initialize DP Array dp[i] = new people on day i dp[1] = 1 (first person) 2 Track Sharers Count active sharers per day share[i] += dp[i-delay] 3 Simulate Days For each day 2 to n: dp[i] = sum of sharers 4 Count Non-Forgotten Sum dp[i] where i+forget > n DP Table Progress Day: 1 2 3 4 5 6 New: 1 0 1 1 1 2 Share: - - 1 1 1 2 Forget: - - - - P1 - Total: 1 1 2 3 3 5 FINAL RESULT People Who Know Secret on Day 6 P1 Forgot P2 OK P3 OK P4 OK P5 OK P6 OK OUTPUT 5 Breakdown: Day 3: 1 | Day 4: 1 Day 5: 1 | Day 6: 2 = 5 Key Insight: Use Dynamic Programming with a sliding window approach. Track new people discovering the secret each day. A person discovered on day i can share from day (i+delay) to day (i+forget-1). At day n, sum all people who discovered within the last 'forget' days. Time: O(n), Space: O(n) TutorialsPoint - Number of People Aware of a Secret | Optimal DP Solution
Asked in
Facebook 15 Amazon 12 Google 8
23.4K Views
Medium Frequency
~25 min Avg. Time
890 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