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
Secret Spreading: n=6, delay=2, forget=4Day:123456New learners:101123Can share:NoNoYesYesYesYesRemember:YesYesYesYesYesNoActive at day 6Days 3,4,5: 1+1+2 = 4Wait delay=2 days before sharingForget after forget=4 daysResult: 5 people know the secret
Understanding the Visualization
1
Input
n=6 days, delay=2 (wait 2 days before sharing), forget=4 (forget after 4 days)
2
Process
Track daily learners and sharing window
3
Output
Count people who still remember at day n
Key Takeaway
🎯 Key Insight: Track daily counts with a sliding window to efficiently manage sharing and forgetting periods
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