Count numbers in range [L, R] that are divisible by square root of a number
Given two numbers L and R, the task is to find the count of all numbers in a given range [L, R] that are divisible by the square root of that number. The square root of a number is the value that, when multiplied by itself, gives the original number.
Note: Here square root of a number represents the floor division. i.e, sqrt(4) = 2, sqrt(10) = 3, sqrt(17) = 4.
Examples:
Input: L = 7, R = 18
Output: 5
Explanation: 8, 9, 12, 15, and 16 are divisible by sqrt(num), where num = [L, R]Input: L = 10, R = 15
Output: 2
Explanation: 12, 15 are divisible by sqrt(num), where num = [L, R]
Approach: To solve the problem follow the below idea:
Let x be a square root of a number, Then x can divide at most three numbers in the range [L, R], as shown in example 1: ‘3’ is a square root which divide 9, 12 and 15 only. So, we can use this to find numbers which are divisible by (square root of a number l) + 1 to (square root of a number l) – 1. and then find separately numbers which are divisible by square root of l and r.
Follow the steps to solve the problem:
- First, find numbers that are divisible by the square root of l.
- Then find numbers that are divisible by the square root of r.
- Then find numbers that are divisible by (square root of a number l) + 1 to (square root of a number l) -1. The count of these numbers will be 3*(sqrt(r)-sqrt(l)) because a number can divide at most three elements as shown in example 1
- Then our final answer will be the sum of all these and then return the answer
Below is the implementation of the above approach:
C++
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Given two numbers L and R, the task is to find the count of all numbers in a given range [L, R] that are divisible by the square root of that number. The square root of a number is the value that, when multiplied by itself, gives the original number.
Note: Here square root of a number represents the floor division. i.e, sqrt(4) = 2, sqrt(10) = 3, sqrt(17) = 4.
Examples:
Input: L = 7, R = 18
Output: 5
Explanation: 8, 9, 12, 15, and 16 are divisible by sqrt(num), where num = [L, R]Input: L = 10, R = 15
Output: 2
Explanation: 12, 15 are divisible by sqrt(num), where num = [L, R]
Approach: To solve the problem follow the below idea:
Let x be a square root of a number, Then x can divide at most three numbers in the range [L, R], as shown in example 1: ‘3’ is a square root which divide 9, 12 and 15 only. So, we can use this to find numbers which are divisible by (square root of a number l) + 1 to (square root of a number l) – 1. and then find separately numbers which are divisible by square root of l and r.
Follow the steps to solve the problem:
- First, find numbers that are divisible by the square root of l.
- Then find numbers that are divisible by the square root of r.
- Then find numbers that are divisible by (square root of a number l) + 1 to (square root of a number l) -1. The count of these numbers will be 3*(sqrt(r)-sqrt(l)) because a number can divide at most three elements as shown in example 1
- Then our final answer will be the sum of all these and then return the answer
Below is the implementation of the above approach:
C++
|
Time Complexity: O(1)
Auxiliary Space: O(1)