Teaching Kids Programming – Count Distinct Numbers on Board (Recursion, Simulation, Math, Hash Set)


Teaching Kids Programming: Videos on Data Structures and Algorithms

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.

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

Hints:
For n > 2, n % (n – 1) == 1 thus n – 1 will be added on the board the next day.
As the operations are performed for so long time, all the numbers lesser than n except 1 will be added to the board.
What will happen if n == 1?

Count Distinct Numbers on Board

The given input N is small, thus we can simulate the process. We use a hash set to store the unique/distinct numbers, and if n % i is 1, we add it to the hash set, and repeat the process for i. We need to check numbers from 2 to N-1 only. The time/space complexity is O(N) as at most there are N-1 numbers put on the whiteboard.

The following is the Recursive algorithm for simulating the process.

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
    def distinctIntegers(self, n: int) -> int:
        
        nums = set([n])
        
        def f(n):
            for i in range(2, n):
                if n % i == 1 and i not in nums:
                    nums.add(i)
                    f(i)
                    
        f(n)
        return len(nums)
class Solution:
    def distinctIntegers(self, n: int) -> int:
        
        nums = set([n])
        
        def f(n):
            for i in range(2, n):
                if n % i == 1 and i not in nums:
                    nums.add(i)
                    f(i)
                    
        f(n)
        return len(nums)

Since we will repeat enough number of times, all the numbers between 2 to N-1 will be eventually on the whiteboard. Consider N which is greater than 2, N%(N-1)==1, and next round, N-2 will be qualified, and then N-3 etc until 2. Special case is when N=1 and in this case, only 1 will be on the board. Thus, if N is 1, the answer is one, otherwise, the answer is N-1. The time/space complexity is O(1) constant since this is just pure math.

1
2
3
class Solution:
    def distinctIntegers(self, n: int) -> int:
        return max(n - 1, 1)
class Solution:
    def distinctIntegers(self, n: int) -> int:
        return max(n - 1, 1)

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
618 words
Last Post: Teaching Kids Programming - Algorithms to Take Max/Min, Square Root, and Compute Sum after K times (Heap, Sort, Index)
Next Post: How to Send Keys using PowerShell or VBScript or JScript via WScript.Shell COM Object (Simulate Keystrokes)?

The Permanent URL is: Teaching Kids Programming – Count Distinct Numbers on Board (Recursion, Simulation, Math, Hash Set)

Leave a Reply