Description:
Given two integers firstNumber and secondNumber and a divisor, count the number of integers within the range [firstNumber, secondNumber] that are divisible by the divisor.
Example 1:
Input: firstNumber = 6, secondNumber = 11, divisor = 2 Output: 3
Example 2:
Input: firstNumber = 0, secondNumber = 0, divisor = 11 Output: 1
Example 3:
Input: firstNumber = 10, secondNumber = 10, divisor = 5 Output: 1
Example 4:
Input: firstNumber = 10, secondNumber = 10, divisor = 7 Output: 0
Algorithmic Steps This problem is solved using simple arithmetic:
- Compute the number of divisible numbers up to
secondNumberby dividing it by the divisor and taking the floor. - Compute the number of divisible numbers before
firstNumberby dividing (firstNumber- 1) by the divisor and taking the floor. - Subtract the two results to get the count of divisible numbers in the range.
Time and Space complexity:
- Time complexity:
O(1). - Space complexity:
O(1).