Number of People Aware of a Secret - Problem

Imagine a viral secret spreading through a community with specific rules! ๐Ÿคซ

On day 1, exactly one person discovers a secret. Here's how the secret spreads:

  • ๐Ÿ“… Each person starts sharing the secret after delay days from when they first learned it
  • ๐Ÿง  Each person forgets the secret exactly forget days after discovering it
  • ๐Ÿ“ข People share the secret with one new person every day (from delay day until they forget)
  • ๐Ÿšซ A person cannot share on the same day they forget or afterwards

Goal: Given integers n, delay, and forget, find how many people know the secret at the end of day n.

Since the answer can be very large, return it modulo 109 + 7.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 6, delay = 2, forget = 4
โ€บ Output: 5
๐Ÿ’ก Note: Day 1: 1 person knows. Day 3: person starts sharing, 1 new person. Day 4: 1 person still sharing, 1 new person. Day 5: original person forgets, but 2 people from days 3,4 know, and day 3 person starts sharing, creating 1 new person. Day 6: people from days 3,4,5 still know, day 4 person starts sharing, creating 1 new person. Total: 5 people know the secret.
example_2.py โ€” No Sharing Case
$ Input: n = 4, delay = 1, forget = 3
โ€บ Output: 6
๐Ÿ’ก Note: Day 1: 1 person. Day 2: original person shares (delay=1), +1 new = 2 total. Day 3: both share, +2 new = 4 total. Day 4: original forgets (forget=3), but 3 people from days 2,3 still know and 2 of them share, +2 new. Total knowing: people from days 2,3,4,4 = 6.
example_3.py โ€” Minimal Case
$ Input: n = 1, delay = 1, forget = 2
โ€บ Output: 1
๐Ÿ’ก Note: Only day 1, so only 1 person knows the secret (the original discoverer). No time for sharing or forgetting.

Visualization

Tap to expand
Secret Lifecycle PhasesLearning PhaseDays 1 to delayCannot share yetSharing PhaseDays delay+1 to forget-1Share with 1 person/dayForgotten PhaseDay forget onwardsNo longer knows secretDP State Trackingdp[i] = Number of people who discovered secret on day i1dp[1] = 1after delay days1dp[delay+1] = 12dp[delay+2] = 23dp[delay+3] = 3Growth pattern dependson sharing vs forgettingdynamicsFinal Answer = ฮฃ dp[i] for i โˆˆ [n-forget+1, n]
Understanding the Visualization
1
Discovery Phase
Person learns the secret, spends 'delay' days practicing/learning
2
Sharing Phase
Person actively shares with 1 new person every day
3
Forgetting Phase
After 'forget' days total, person moves on and stops caring
4
DP Optimization
Instead of tracking individuals, track counts per discovery day
Key Takeaway
๐ŸŽฏ Key Insight: Track discovery counts per day instead of individual people to achieve optimal O(n) complexity while maintaining accurate state transitions.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through days 1 to n, with constant work per day

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

DP array of size n to store discovery counts per day

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค n โ‰ค 1000
  • 1 โ‰ค delay < forget โ‰ค n
  • Answer should be returned modulo 109 + 7
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
840 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