VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-elements-which-divide-all-numbers-in-range-l-r/

⇱ Count elements which divide all numbers in range L-R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count elements which divide all numbers in range L-R

Last Updated : 11 Jul, 2025

The problem statement describes a scenario where we are given an array of N numbers and Q queries. Each query consists of two integers L and R, representing a range of indices in the array. The task is to find the count of numbers in the array that divide all the numbers in the range L-R.

Examples : 

Input : a = {3, 4, 2, 2, 4, 6} 
 Q = 2
 L = 1 R = 4 
 L = 2 R = 6
Output : 0
 2 
Explanation : The range 1-4 has {3, 4, 2, 2} 
which does not have any number that divides all the 
numbers in this range. 
The range 2-6 has {4, 2, 2, 4, 6} which has 2 numbers {2, 2} which divides 
all numbers in the given range. 

Input: a = {1, 2, 3, 5} 
 Q = 2 
 L = 1 R = 4 
 L = 2 R = 4 
Output: 1 
 0 

Naive approach : Iterate from range L-R for every query and check if the given element at index-i divide all the numbers in the range. We keep a count for of all the elements which divides all the numbers. The complexity of every query at worst case will be O(n2).

Below is the implementation of Naive Approach :  


Output
1
0

Time Complexity:-The time complexity of the given Java program is O(n^2) in the worst case, where n is the length of the input array. This is because the program has two nested loops, each iterating over the input array, resulting in a time complexity of O(n^2) for the worst case.

Space Complexity:-The space complexity of the program is O(1), which means that the amount of memory used by the program is constant, regardless of the input size. This is because the program only uses a fixed amount of memory for storing variables and does not create any new data structures or objects that grow in size with the input size.

Efficient approach : 

Use Segment Trees to solve this problem. If an element divides all the numbers in a given range, then the element is the minimum number in that range and it is the gcd of all elements in the given range L-R. So the count of the number of minimums in range L-R, given that minimum is equal to the gcd of that range will be our answer to every query. The problem boils down to finding the GCD, MINIMUM and countMINIMUM for every range using Segment trees. On every node of the tree, three values are stored. 

On querying for a given range, if the gcd and minimum of the given range are equal, countMINIMUM is returned as the answer. If they are unequal, 0 is returned as the answer.

Below is the implementation of efficient approach : 


Output
0
2

Time Complexity: Time Complexity for tree construction is O(n logn) since tree construction takes O(n) and finding out gcd takes O(log n). The time taken for every query in worst case will be O(log n * log n) since the inbuilt function __gcd takes O(log n)

Space Complexity: O(n)

Comment