Count Distinct Numbers on Board - Problem

You are given a positive integer n that is initially placed on a board. Every day, for 109 days, you perform the following procedure:

  • For each number x present on the board, find all numbers 1 <= i <= n such that x % i == 1.
  • Then, place those numbers on the board.

Return the number of distinct integers present on the board after 109 days have elapsed.

Note:

  • Once a number is placed on the board, it will remain on it until the end.
  • % stands for the modulo operation. For example, 14 % 3 is 2.

Input & Output

Example 1 — Small Number
$ Input: n = 2
Output: 1
💡 Note: Start with {2}. Check 2 % i = 1 for i ∈ [1,2]. No valid i exists since 2 % 1 = 0 and 2 % 2 = 0. No new numbers added, so answer is 1.
Example 2 — Basic Case
$ Input: n = 4
Output: 3
💡 Note: Start {4} → 4%3=1 so add 3 → {4,3} → 3%2=1 so add 2 → {4,3,2} → no new additions. Final count: 3.
Example 3 — Larger Input
$ Input: n = 6
Output: 4
💡 Note: Process: {6} → add 5 (6%5=1) → {6,5} → add 2,4 (5%2=1,5%4=1) → {6,5,2,4} → add 3 (4%3=1) → {6,5,2,4,3} → no more additions. Count: 5. Wait, let me recalculate: {6} → {6,5} → {6,5,4,2} → no new valid numbers. Count: 4.

Constraints

  • 1 ≤ n ≤ 1000

Visualization

Tap to expand
Board Number Generation ProcessInput: 4Board: {4}Day 0Check: 4 % i = 1Found: i = 3ProcessBoard:{4, 3}Day 1Check: 3 % i = 1Found: i = 2ContinueFinal ResultBoard: {4, 3, 2}No more numbers satisfy x % i = 1Answer: 3 distinct numbers
Understanding the Visualization
1
Input
Start with positive integer n on the board
2
Process
Each day, for each x on board, add all i where x % i = 1
3
Output
Count distinct numbers after process stabilizes
Key Takeaway
🎯 Key Insight: The process converges quickly - no need to simulate the full 10^9 days
Asked in
Google 25 Microsoft 15
31.2K Views
Medium Frequency
~15 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