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.
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
First, you should compute the Greatest Common Divisor(gcd) of all the initial numbers. Because the queries consist of dividing a number to one of its divisors it means the 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.
12 6 6
Time Complexity : O(m + n)
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Divide every element of one array by other array elements
- Find the Initial Array from given array after range sum queries
- Print modified array after multiple array range increment operations
- Divide an array into K subarray with the given condition
- Divide array into two sub-arrays such that their averages are equal
- Count of integers that divide all the elements of the given array
- Divide the array in K segments such that the sum of minimums is maximized
- Divide binary array into three equal parts with same value
- Array range queries over range queries
- Number of ways to divide an array into K equal sum sub-arrays
- Count number of ways to divide an array into two halves with same sum
- Find all numbers that divide maximum array elements
- Sum of even values and update queries on an array
- Queries for counts of multiples in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.