Count Distinct Numbers on Board — LeetCode 2549 Python Solution

EasyArrayHash TableMathSimulation
Problem
#2549
Reading time
2 min

The 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.

Example

Input
n = 5
Output
4
Explanation
Initially, 5 is present on the board.

Python solution

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

Complexity

MeasureComplexity
TimeO(1)
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 2549. Count Distinct Numbers on Board is filed here because LeetCode tags it Math, which is the vocabulary this hub collects.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2549. Count Distinct Numbers on Board?
LeetCode 2549. Count Distinct Numbers on Board is rated Easy on LeetCode.
What is the time complexity of LeetCode 2549. Count Distinct Numbers on Board?
The Python solution on this page runs in O(1).
What is the space complexity of LeetCode 2549. Count Distinct Numbers on Board?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2549. Count Distinct Numbers on Board cover?
LeetCode 2549. Count Distinct Numbers on Board is tagged Array, Hash Table, Math and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview