You are given an integer n
, representing the size of a board with numbers ranging from 1 to n
. The task is to count the number of distinct numbers on this board.
Write a function countDistinctNumbers(n: int) -> int
that returns the count of distinct numbers on the board.
n
.n
?
n
will be a positive integer. For edge cases, we should consider boundary values.n
, the complexity should be optimal.Since the numbers on the board range from 1
to n
, and each number appears exactly once, the task is straightforward:
1
to n
.n
.Here is the Python function to achieve this:
def countDistinctNumbers(n: int) -> int:
return n
This solution is optimal as the number of distinct numbers in the given range from 1
to n
is inherently equal to n
.
countDistinctNumbers(5) # Output: 5
countDistinctNumbers(10) # Output: 10
countDistinctNumbers(1) # Output: 1
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?