Data Structures19 sections · 729 units
Open in Course

Problem - Range GCD Queries

GCD is idempotent

Given an array, answer queries for gcd⁡\gcd of elements in range [l,r][l, r].

Example: arr =[2,6,9,3,6]= [2, 6, 9, 3, 6]

  • Query (0,2)(0, 2): gcd⁡(2,6,9)=1\gcd(2, 6, 9) = 1
  • Query (1,4)(1, 4): gcd⁡(6,9,3,6)=3\gcd(6, 9, 3, 6) = 3 gcd⁡\gcd is idempotent: gcd⁡(a,a)=a\gcd(a, a) = a.

So the overlap trick works. Just change the merge operation from min⁡\min to gcd⁡\gcd. Constraints: up to 10510^5 elements, up to 10510^5 queries.