You are given an array of N integer values and M update operations. An update consists of choosing an element of the array and dividing it by a given value. It is guaranteed that the element is divisible by the chosen value. After each update, you should compute the greatest common divisor of all the elements of the array.
Examples:
Input : 3 3
36 24 72
1 3
3 12
2 4
Output :12
6
6
After each operation the array values will be:
1. 12, 24, 72
2. 12, 24, 6
3. 12, 6, 6
Input :5 6
100 150 200 600 300
4 6
2 3
4 4
1 4
2 5
5 25
Output : 50
50
25
25
5
1
Approach: First, you should compute the Greatest Common Divisor(gcd) of all the initial numbers. Because the queries consist of dividing a number by one of its divisors it means that after each query the new gcd is a divisor of the old gcd. So for each query, you should simply compute the gcd between the updated value and the previous gcd.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void print_gcd_online( int n, int m,
int query[][2], int arr[])
{
int max_gcd = 0;
int i = 0;
for (i = 0; i < n; i++)
max_gcd = __gcd(max_gcd, arr[i]);
for (i = 0; i < m; i++)
{
query[i][0]--;
arr[query[i][0]] /= query[i][1];
max_gcd = __gcd(arr[query[i][0]], max_gcd);
cout << max_gcd << endl;
}
}
int main()
{
int n = 3;
int m = 3;
int query[m][2];
int arr[] = {36, 24, 72};
query[0][0] = 1;
query[0][1] = 3;
query[1][0] = 3;
query[1][1] = 12;
query[2][0] = 2;
query[2][1] = 4;
print_gcd_online(n, m, query, arr);
return 0;
}
|
Java
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static void print_gcd_online( int n, int m,
int [][] query, int [] arr)
{
int max_gcd = 0 ;
int i = 0 ;
for (i = 0 ; i < n; i++)
max_gcd = gcd(max_gcd, arr[i]);
for (i = 0 ; i < m; i++) {
query[i][ 0 ]--;
arr[query[i][ 0 ]] /= query[i][ 1 ];
max_gcd = gcd(arr[query[i][ 0 ]], max_gcd);
System.out.println(max_gcd);
}
}
public static void main(String[] args)
{
int n = 3 ;
int m = 3 ;
int [][] query = new int [m][ 2 ];
int [] arr = new int [] { 36 , 24 , 72 };
query[ 0 ][ 0 ] = 1 ;
query[ 0 ][ 1 ] = 3 ;
query[ 1 ][ 0 ] = 3 ;
query[ 1 ][ 1 ] = 12 ;
query[ 2 ][ 0 ] = 2 ;
query[ 2 ][ 1 ] = 4 ;
print_gcd_online(n, m, query, arr);
}
}
|
Python3
def gcd(a, b):
if a = = 0 :
return b
return gcd(b % a, a)
def print_gcd_online(n, m, query, arr):
max_gcd = 0
for i in range ( 0 , n):
max_gcd = gcd(max_gcd, arr[i])
for i in range ( 0 , m):
query[i][ 0 ] - = 1
arr[query[i][ 0 ]] / / = query[i][ 1 ]
max_gcd = gcd(arr[query[i][ 0 ]], max_gcd)
print (max_gcd)
if __name__ = = "__main__" :
n, m = 3 , 3
query = [[ 1 , 3 ], [ 3 , 12 ], [ 2 , 4 ]]
arr = [ 36 , 24 , 72 ]
print_gcd_online(n, m, query, arr)
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static void print_gcd_online( int n, int m,
int [,] query,
int [] arr)
{
int max_gcd = 0;
int i = 0;
for (i = 0; i < n; i++)
max_gcd = gcd(max_gcd, arr[i]);
for (i = 0; i < m; i++)
{
query[i,0]--;
arr[query[i, 0]] /= query[i, 1];
max_gcd = gcd(arr[query[i, 0]], max_gcd);
Console.WriteLine(max_gcd);
}
}
public static void Main()
{
int n = 3;
int m = 3;
int [,] query = new int [m, 2];
int [] arr = new int [] { 36, 24, 72 };
query[0, 0] = 1;
query[0, 1] = 3;
query[1, 0] = 3;
query[1, 1] = 12;
query[2, 0] = 2;
query[2, 1] = 4;
print_gcd_online(n, m, query, arr);
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function print_gcd_online( $n , $m ,
$query , $arr )
{
$max_gcd = 0;
$i = 0;
for ( $i = 0; $i < $n ; $i ++)
$max_gcd = gcd( $max_gcd ,
$arr [ $i ]);
for ( $i = 0; $i < $m ; $i ++)
{
$query [ $i ][0]--;
$arr [ $query [ $i ][0]] /= $query [ $i ][1];
$max_gcd = gcd( $arr [ $query [ $i ][0]],
$max_gcd );
echo ( $max_gcd ), "\n" ;
}
}
$n = 3; $m = 3; $query ;
$arr = array ( 36, 24, 72 );
$query [0][0] = 1; $query [0][1] = 3;
$query [1][0] = 3; $query [1][1] = 12;
$query [2][0] = 2; $query [2][1] = 4;
print_gcd_online( $n , $m , $query , $arr );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
function print_gcd_online(n,m,query,arr)
{
let max_gcd = 0;
let i = 0;
for (i = 0; i < n; i++)
max_gcd = gcd(max_gcd, arr[i]);
for (i = 0; i < m; i++)
{
query[i][0]--;
arr[query[i][0]] /= query[i][1];
max_gcd = gcd(arr[query[i][0]], max_gcd);
document.write(max_gcd + "</br>" );
}
}
let n = 3;
let m = 3;
let query = new Array(m);
for (let i = 0; i < m; i++)
{
query[i] = new Array(2);
for (let j = 0; j < 2; j++)
{
query[i][j] = 0;
}
}
let arr = [36, 24, 72];
query[0][0] = 1;
query[0][1] = 3;
query[1][0] = 3;
query[1][1] = 12;
query[2][0] = 2;
query[2][1] = 4;
print_gcd_online(n, m, query, arr);
</script>
|
Complexity Analysis:
- Time Complexity : O(m + n)
- Auxiliary Space: O(1)