Count Distinct Numbers on Board
EASYDescription
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
xpresent on the board, find all numbers1 <= i <= nsuch thatx % 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 % 3is2.
Example 1:
Input: n = 5 Output: 4 Explanation: Initially, 5 is present on the board. The next day, 2 and 4 will be added since 5 % 2 == 1 and 5 % 4 == 1. After that day, 3 will be added to the board because 4 % 3 == 1. At the end of a billion days, the distinct numbers on the board will be 2, 3, 4, and 5.
Example 2:
Input: n = 3 Output: 2 Explanation: Since 3 % 2 == 1, 2 will be added to the board. After a billion days, the only two distinct numbers on the board are 2 and 3.
Constraints:
1 <= n <= 100
Approaches
Checkout 3 different approaches to solve Count Distinct Numbers on Board. Click on different approaches to view the approach and algorithm in detail.
Constant Time Mathematical Approach
The most efficient approach comes from a mathematical observation of the problem's rules. By analyzing the condition x % i == 1, we can deduce a pattern that reveals the final state of the board without any simulation. The large number of days (10^9) is a strong hint that the system reaches a predictable fixed point quickly.
Algorithm
- Analyze the condition
x % i == 1. This impliesiis a divisor ofx - 1andi > 1. - If
n > 2, thenn-1is a valid choice forisincen % (n-1) == 1and2 <= n-1 <= n. Thus,n-1is added to the board. - If a number
k >= 3is on the board,k-1will be added becausek % (k-1) == 1. - This creates a chain reaction:
nplacesn-1,n-1placesn-2, ...,3places2. - The chain stops at
2, as2-1=1has no divisors greater than 1. - So for
n > 2, all numbers from2tonwill eventually be on the board. The count isn-1. - For
n=1orn=2, this chain reaction does not start. The board will only contain the initial number. The count is 1. - The final logic is: if
n <= 2, return 1; otherwise, returnn-1.
Let's analyze the core rule: a number i is placed on the board if x % i == 1 for some x already on the board. This is only possible if i > 1.
Case 1: n > 2
- Initially,
nis on the board. Let's check ifn-1can be added. We testx=nandi=n-1. The condition isn % (n-1) == 1, which is true. Sincen > 2, we haven-1 >= 2, soi=n-1is a valid number to be placed on the board. - Now,
n-1is on the board. Ifn-1 > 2(i.e.,n > 3), we can similarly show thatn-2will be added. - This establishes a chain reaction:
ncausesn-1to be added, which causesn-2to be added, and so on, all the way down to3causing2to be added. - Once
2is on the board, it cannot add any new numbers, because2 % i == 1requiresito be a divisor of2-1=1, and no suchi > 1exists. - Therefore, for any
n > 2, the board will eventually contain all integers from2ton. The total count of these numbers isn - 2 + 1 = n-1.
Case 2: n <= 2
- If
n=1orn=2, the chain reaction described above never starts. No numberi > 1satisfiesn % i == 1. Thus, the board only ever contains the initial number. The count is 1.
This leads to a very simple O(1) solution.
class Solution {
public int distinctIntegers(int n) {
// If n is 1 or 2, no new numbers can be added.
// For n=1, 1%i==1 is impossible.
// For n=2, 2%i==1 requires i to be a divisor of 1, so i=1, but we need i>1.
if (n <= 2) {
return 1;
}
// If n > 2, n-1 is added, which adds n-2, ..., until 2 is added.
// The final set of numbers is {2, 3, ..., n}.
// The size of this set is n-1.
return n - 1;
}
}
Complexity Analysis
Pros and Cons
- Extremely efficient with constant time and space complexity.
- Provides a complete solution by understanding the problem's core mechanics.
- Simple and elegant implementation.
- Requires a logical deduction and mathematical insight rather than direct implementation of the problem statement.
Code Solutions
Checking out 3 solutions in different languages for Count Distinct Numbers on Board. Click on different languages to view the code.
class Solution {
public
int distinctIntegers(int n) { return Math.max(1, n - 1); }
}
Video Solution
Watch the video walkthrough for Count Distinct Numbers on Board
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.