Can anyone please explain how to solve the GCDQ PROBLEM(https://www.codechef.com/problems/GCDQ) ?
Asked by: arkaprava_roy on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.
In this question, for each query you have to find the greatest common divisor of array excluding the range [l, r]. So you have to precompute prefix gcd array as well as suffix gcd array.
Prefix[i] will store greatest common divisor of all elements in the range [0,i] i.e. Prefix[i] = gcd(Prefix[i-1],A[i]).
Similarly Suffix[i] will store greatest common divisor of all elements in the range [i, n-1].
Suffix[i] = gcd(Suffix[i+1],A[i]).
Hence, if range given in the query is [l,r] then gcd of array excluding the range [l, r] will be equal to gcd(Prefix[l-1],Suffix[r+1]).