VOOZH about

URL: https://www.geeksforgeeks.org/dsa/gcds-of-a-given-index-ranges-in-an-array/

⇱ GCDs of given index ranges in an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

GCDs of given index ranges in an Array

Last Updated : 23 Jul, 2025

Given an array arr[] of size N and Q queries of type {qs, qe} where qs and qe denote the starting and ending index of the query, the task is to find the GCD of all the numbers in the range.

Examples:

Input: arr[] = {2, 3, 60, 90, 50};
Index Ranges: {1, 3}, {2, 4}, {0, 2}
Output: GCDs of given ranges are 3, 10, 1
Explanation: Elements in the range [1, 3] are {3, 60, 90}.
The GCD of the numbers is 3.
Elements in the range [2, 4] are {60, 90, 50}.
The GCD of the numbers is 10.
Elements in the range [0, 2] are {2, 3, 60}.
The GCD of the numbers is 1 as 2 and 3 are co-prime.

Naive Approach:

A simple solution is to run a loop from qs to qe for every query and find GCD in the given range. Time required to find gcd of all the elements from qs to qe will be O(N*log(Ai)) i.e do a linear scan and find the gcd of each adjacent pair in O(log(Ai))
So, the overall time complexity will be O(Q*N*log(Ai)).

Time Complexity: O(Q*N*log(Ai))
Auxiliary Space: O(1)

GCD of Range using 2D Array:

Another approach is to create a 2D array where an entry [i, j] stores the GCD of elements in range arr[i . . . j]. GCD of a given range can now be calculated in O(1) time. 

Time Complexity: O(N2 + Q) preprocessing takes O(N2) time and O(Q) time to answer Q queries.
Auxiliary Space: O(N2)

GCD of range using Segment Tree:

Prerequisites: Segment Tree Set 1, Segment Tree Set 2 

Segment tree can be used to do preprocessing and query in moderate time. With a segment tree, we can store the GCD of a segment and use that later on for calculating the GCD of given range. 

This can be divided into the following steps:

Representation of Segment tree

  • Leaf Nodes are the elements of the input array.
  • Each internal node represents the GCD of all leaves under it.
  • Array representation of the tree is used to represent Segment Trees i.e., for each node at index i,
    • The Left child is at index 2*i+1
    • Right child at 2*i+2 and 
    • the parent is at floor((i-1)/2).

Construction of Segment Tree from the given array

  • Begin with a segment arr[0 . . . n-1] and keep dividing into two halves (if it has not yet become a segment of length 1), 
    • Call the same procedure on both halves,.
      • Each parent node will store the value of GCD(left node, right node).

Query for GCD of given range

  • For every query, move to the left and right halves of the tree. 
    • Whenever the given range completely overlaps any halve of a tree, return the node from that half without traversing further in that region. 
    • When a halve of the tree completely lies outside the given range, return 0 (as GCD(0, x) = x). 
    • On partial overlapping of range, traverse in left and right halves and return accordingly.

Below is the implementation of the above approach. 

Output:

 GCD of the given range is: 3

Time Complexity: 

  • Time Complexity for tree construction is O(N * log(min(a, b))), where N is the number of modes and a and b are nodes whose GCD is calculated during the merge operation. 
  • Time complexity for each to query is O(log N * log(min(a, b)))

Auxiliary Space: O(N)

Comment