Given an array of n numbers and a number of queries are also given. Each query will be represented by two integers l, r. The task is to find out the GCD of all the numbers of the array excluding the numbers given in the range l, r (both inclusive) for each query.

Examples:

Input : arr[] = {2, 6, 9} Ranges [0 0], [1 1], [1 2] Output : 3 1 2 GCD of numbers excluding [0 0] or first element is GCD(6, 9) = 3 GCD of numbers excluding the [1 1] or second element is GCD(2, 9) = 1 GCD of numbers excluding [1 2] is equal to first element = 2

**Note :** We use 1 based indexing in below explanation

We start from the very basic question how to calculate GCD of two numbers the best choice is Euclid’s algorithm . Now how to calculate GCD of more than one numbers, the solution is simple suppose there are three numbers a, b and c. GCD(a, b, c) = GCD(GCD(a, b), c). In this way, we can easily find out GCD of any number of numbers.

One **simple way** to solve the question for each query suppose the range given is l and r. Take GCD of the numbers from 1 to l-1 suppose it is x then take GCD of the numbers from the range r+1 to n let it be y the output of each query will be GCD (x, y).

An **efficient solution** is to use two arrays, one as a prefix array and the second one as suffix array. The i-th index of prefix array will store GCD of the numbers from 1 to i and the ith index of suffix array will denote the GCD of the numbers from i to n. Now suppose in a particular query range given is l, r it is obvious that the output for that query will be GCD(prefix[l-1], suffix[r+1]).

// C++ program for queries of GCD excluding // given range of elements. #include<bits/stdc++.h> using namespace std; // Calculating GCD using euclid algorithm int GCD(int a, int b) { if (b==0) return a; return GCD (b, a%b); } // Filling the prefix and suffix array void FillPrefixSuffix(int prefix[], int arr[], int suffix[], int n) { // Filling the prefix array following relation // prefix(i) = GCD(prefix(i-1), arr(i)) prefix[0] = arr[0]; for (int i=1 ;i<n; i++) prefix[i] = GCD (prefix[i-1], arr[i]); // Filling the suffix array folowing the // relation suffix(i) = GCD(suffix(i+1), arr(i)) suffix[n-1] = arr[n-1]; for (int i=n-2; i>=0 ;i--) suffix[i] = GCD (suffix[i+1], arr[i]); } // To calculate gcd of the numbers outside range int GCDoutsideRange(int l, int r, int prefix[], int suffix[], int n) { // If l=0, we need to tell GCD of numbers // from r+1 to n if (l==0) return suffix[r+1]; // If r=n-1 we need to return the gcd of // numbers from 1 to l if (r==n-1) return prefix[l-1]; return GCD(prefix[l-1], suffix[r+1]); } // Driver function int main() { int arr[] = {2, 6, 9}; int n = sizeof(arr)/sizeof(arr[0]); int prefix[n], suffix[n]; FillPrefixSuffix(prefix, arr, suffix, n); int l = 0, r = 0; cout << GCDoutsideRange(l, r, prefix, suffix, n) << endl; l = 1 ; r = 1; cout << GCDoutsideRange(l, r, prefix, suffix, n) << endl; l = 1 ; r = 2; cout << GCDoutsideRange(l, r, prefix, suffix, n) << endl; return 0; }

Output:

3 1 2

This article is contributed by **Ayush Jha**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.