Queries for GCD of all numbers of an array except elements in a given range
Last Updated :
04 Jun, 2022
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 to 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 the 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++
// C++ program for queries of GCD excluding
// given range of elements.
#include<bits/stdc++.h>
using namespace std;
// 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 following 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;
}
Java
// Java program for queries of GCD excluding
// given range of elements.
import java.util.*;
class GFG {
// Calculating GCD using euclid algorithm
static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
// Filling the prefix and suffix array
static 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 following 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
static 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 code
public static void main(String[] args) {
int arr[] = {2, 6, 9};
int n = arr.length;
int prefix[] = new int[n];
int suffix[] = new int[n];
FillPrefixSuffix(prefix, arr, suffix, n);
int l = 0, r = 0;
System.out.println(GCDoutsideRange
(l, r, prefix, suffix, n));
l = 1;
r = 1;
System.out.println(GCDoutsideRange
(l, r, prefix, suffix, n));
l = 1;
r = 2;
System.out.println(GCDoutsideRange
(l, r, prefix, suffix, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program for
# queries of GCD excluding
# given range of elements.
# Calculating GCD
# using euclid algorithm
def GCD(a,b):
if (b==0):
return a
return GCD (b, a%b)
# Filling the prefix
# and suffix array
def FillPrefixSuffix(prefix,arr,suffix,n):
# Filling the prefix array
# following relation
# prefix(i) = GCD(prefix(i-1), arr(i))
prefix[0] = arr[0]
for i in range(1,n):
prefix[i] = GCD (prefix[i-1], arr[i])
# Filling the suffix
# array following the
# relation suffix(i) = GCD(suffix(i+1), arr(i))
suffix[n-1] = arr[n-1]
for i in range(n-2,-1,-1):
suffix[i] = GCD (suffix[i+1], arr[i])
# To calculate gcd of
# the numbers outside range
def GCDoutsideRange(l,r,prefix,suffix,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 code
arr = [2, 6, 9]
n = len(arr)
prefix=[]
suffix=[]
for i in range(n+1):
prefix.append(0)
suffix.append(0)
FillPrefixSuffix(prefix, arr, suffix, n)
l = 0
r = 0
print(GCDoutsideRange(l, r, prefix, suffix, n))
l = 1
r = 1
print(GCDoutsideRange(l, r, prefix, suffix, n))
l = 1
r = 2
print(GCDoutsideRange(l, r, prefix, suffix, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program for queries of GCD
// excluding given range of elements.
using System;
class GFG {
// Calculating GCD using
// euclid algorithm
static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
// Filling the prefix and suffix array
static 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 following
// 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
static 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 Code
public static void Main()
{
int []arr = {2, 6, 9};
int n = arr.Length;
int []prefix = new int[n];
int []suffix = new int[n];
FillPrefixSuffix(prefix, arr, suffix, n);
int l = 0, r = 0;
Console.WriteLine(GCDoutsideRange
(l, r, prefix, suffix, n));
l = 1;
r = 1;
Console.WriteLine(GCDoutsideRange
(l, r, prefix, suffix, n));
l = 1;
r = 2;
Console.Write(GCDoutsideRange
(l, r, prefix, suffix, n));
}
}
// This code is contributed by Nitin Mittal.
PHP
<?php
// PHP program for queries of GCD excluding
// given range of elements.
// Calculating GCD using euclid algorithm
function GCD($a, $b)
{
if ($b == 0)
return $a;
return GCD ($b, $a % $b);
}
// Filling the prefix and suffix array
function FillPrefixSuffix(&$prefix, &$arr, &$suffix, $n)
{
// Filling the prefix array following relation
// prefix(i) = GCD(prefix(i-1), arr(i))
$prefix[0] = $arr[0];
for ($i = 1; $i < $n; $i++)
$prefix[$i] = GCD ($prefix[$i - 1], $arr[$i]);
// Filling the suffix array following the
// relation suffix(i) = GCD(suffix(i+1), arr(i))
$suffix[$n - 1] = $arr[$n - 1];
for ($i = $n - 2; $i >= 0 ;$i--)
$suffix[$i] = GCD ($suffix[$i + 1], $arr[$i]);
}
// To calculate gcd of the numbers outside range
function GCDoutsideRange($l, $r, &$prefix, &$suffix, $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 Code
$arr = array(2, 6, 9);
$n = sizeof($arr);
$prefix = array_fill(0, $n, NULL);
$suffix = array_fill(0, $n, NULL);
FillPrefixSuffix($prefix, $arr, $suffix, $n);
$l = 0;
$r = 0;
echo GCDoutsideRange($l, $r, $prefix, $suffix, $n) . "\n";
$l = 1 ;
$r = 1;
echo GCDoutsideRange($l, $r, $prefix, $suffix, $n) . "\n";
$l = 1 ;
$r = 2;
echo GCDoutsideRange($l, $r, $prefix, $suffix, $n) . "\n";
// This code is contributed by ita_c
?>
JavaScript
<script>
// JavaScript program for queries of GCD excluding
// given range of elements.
// Calculating GCD using euclid algorithm
function GCD(a , b) {
if (b == 0)
return a;
return GCD(b, a % b);
}
// Filling the prefix and suffix array
function FillPrefixSuffix(prefix , arr , suffix , n) {
// Filling the prefix array following relation
// prefix(i) = GCD(prefix(i-1), arr(i))
prefix[0] = arr[0];
for (i = 1; i < n; i++)
prefix[i] = GCD(prefix[i - 1], arr[i]);
// Filling the suffix array following the
// relation suffix(i) = GCD(suffix(i+1), arr(i))
suffix[n - 1] = arr[n - 1];
for (i = n - 2; i >= 0; i--)
suffix[i] = GCD(suffix[i + 1], arr[i]);
}
// To calculate gcd of the numbers outside range
function GCDoutsideRange(l , r , prefix , suffix , 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 code
var arr = [ 2, 6, 9 ];
var n = arr.length;
var prefix = Array(n).fill(0);
var suffix = Array(n).fill(0);
FillPrefixSuffix(prefix, arr, suffix, n);
var l = 0, r = 0;
document.write(GCDoutsideRange(l, r, prefix, suffix, n));
document.write("<br>");
l = 1;
r = 1;
document.write(GCDoutsideRange(l, r, prefix, suffix, n));
document.write("<br>");
l = 1;
r = 2;
document.write(GCDoutsideRange(l, r, prefix, suffix, n));
document.write("<br>");
// This code is contributed by todaysgaurav
</script>
Output:
3
1
2
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Similar Reads
GCD (Greatest Common Divisor) Practice Problems for Competitive Programming
GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest positive integer that divides both of the numbers.GCD of Two NumbersFastest Way to Compute GCDThe fastest way to find the Greatest Common Divisor (GCD) of two numbers is by using the Euclidean algorithm. The E
4 min read
Program to Find GCD or HCF of Two Numbers
Given two numbers a and b, the task is to find the GCD of the two numbers.Note: The GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. Examples:Input: a = 20, b = 28Output: 4Explanation: The factors of 20 are 1, 2, 4, 5, 10 an
15+ min read
Check if two numbers are co-prime or not
Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples : Input : 2 3Output : Co-PrimeInput : 4 8Output : Not Co-PrimeThe idea is simple, we find GCD of two numbers a
5 min read
GCD of more than two (or array) numbers
Given an array arr[] of non-negative numbers, the task is to find GCD of all the array elements. In a previous post we find GCD of two number.Examples:Input: arr[] = [1, 2, 3]Output: 1Input: arr[] = [2, 4, 6, 8]Output: 2Using Recursive GCDThe GCD of three or more numbers equals the product of the pr
11 min read
Program to find LCM of two numbers
LCM of two numbers is the smallest number which can be divided by both numbers. Input : a = 12, b = 18Output : 3636 is the smallest number divisible by both 12 and 18Input : a = 5, b = 11Output : 5555 is the smallest number divisible by both 5 and 11[Naive Approach] Using Conditional Loop This appro
8 min read
LCM of given array elements
In this article, we will learn how to find the LCM of given array elements.Given an array of n numbers, find the LCM of it. Example:Input : {1, 2, 8, 3}Output : 24LCM of 1, 2, 8 and 3 is 24Input : {2, 7, 3, 9, 4}Output : 252Table of Content[Naive Approach] Iterative LCM Calculation - O(n * log(min(a
14 min read
Find the other number when LCM and HCF given
Given a number A and L.C.M and H.C.F. The task is to determine the other number B. Examples: Input: A = 10, Lcm = 10, Hcf = 50. Output: B = 50 Input: A = 5, Lcm = 25, Hcf = 4. Output: B = 20 Formula: A * B = LCM * HCF B = (LCM * HCF)/AExample : A = 15, B = 12 HCF = 3, LCM = 60 We can see that 3 * 60
4 min read
Minimum insertions to make a Co-prime array
Given an array of N elements, find the minimum number of insertions to convert the given array into a co-prime array. Print the resultant array also.Co-prime Array : An array in which every pair of adjacent elements are co-primes. i.e, gcd(a, b) = 1 . Examples : Input : A[] = {2, 7, 28}Output : 1Exp
6 min read
Find the minimum possible health of the winning player
Given an array health[] where health[i] is the health of the ith player in a game, any player can attack any other player in the game. The health of the player being attacked will be reduced by the amount of health the attacking player has. The task is to find the minimum possible health of the winn
4 min read
Minimum squares to evenly cut a rectangle
Given a rectangular sheet of length l and width w. we need to divide this sheet into square sheets such that the number of square sheets should be as minimum as possible.Examples: Input :l= 4 w=6 Output :6 We can form squares with side of 1 unit, But the number of squares will be 24, this is not min
4 min read