This is Range query question in which we have been provided with and array of size N. Given are 3 types of queries and you have to answer M number of specified queries.

**Type 1 query :**You will be given 3 values in the form of L R X and in this type of query to have to multiply x to the array elements inclusive in the range L to R.

**Type 2 query :**In this query also you will be given 3 values in the form of L R Y and after executing this type of query you will replace the array elements in the form that the first element is replaced by Y, the second element is replaced by 2*Y and as follows inclusive in the range L to R.

**Type 3 query :** In this you will be given 2 value L and R and in this you have to

find the product of all numbers in the range. As this number could be very large, you have to just find the number of trailing zeros of this number when represented in decimal notation.

Examples:

Input : arr[] = {2, 4, 3, 5, 5| queries[] = {{3 2 4}, {3 2 5}, {2 2 4 1}, {1 3 3 10}, {3 1 5}} Output : 5 Explanation : Since the first query is of type 3 so we multiply the elements 4 * 3 * 5 = 60. Since the second query is of type 3 so we multiply the elements 4 * 3 * 5 * 5 = 300. Since the third query is of type 2 and the value of Y is 1 so after execution of this query the array becomes [2, 1, 2, 3, 5]. Since the fourth query is of type 1 and the value of x is 10 so after execution of this query the array becomes [2, 1, 20, 3, 5]. Now the last query is of type 3 then we simply multiply all the elements inclusive in the given range i.e. 2 * 1 * 20 * 3 * 5 = 600. Now our task is to calculate the trailing zeros obtained in the type 3 query i.e. 60 has 1 trailing zero, 300 has 2 trailing zeros and 600 has 2 trailing zeros so the answer of this given input is 5.

**Method 1:**

In this we can simply apply the Brute force method. In the brute force method we will apply all the operation in the array elements and for every type 3 query we will store the obtained result in a new array then we will calculate the number of trailing zeros for every result thus obtained and then calculate the desired sum.

The complexity of this method will be O(m*n) as we will operate the entire array m times for the given m queries and an extra space of size m will be required to save the results obtained in the type 3 queries for calculating the number of trailing zeros after execution of m queries.

So, time complexity is O(m*n) and space complexity is O(m).

**Method 2:**

In this method we have 2 vectors because a number with trailing zero can be multiple of 10 and 10 is a multiple of 2 and 5 so two separate vectors have been maintained for this purpose. And the rest has been explained below.

`// CPP program to solve three types of queries. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `//vector of 1000 elements, ` `//all set to 0 ` `vector<` `int` `> twos(1000,0); ` ` ` `//vector of 1000 elements, ` `//all set to 0 ` `vector<` `int` `> fives(1000,0); ` ` ` `int` `sum = 0; ` ` ` `// Function to check number of ` `// trailing zeros in multiple of 2 ` `int` `returnTwos(` `int` `val) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(val % 2 == 0 && val != 0) { ` ` ` ` ` `val = val / 2; ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Function to check number of ` `// trailing zeros in multiple of 5 ` `int` `returnFives(` `int` `val) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(val % 5 == 0 && val != 0) { ` ` ` ` ` `val = val / 5; ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Function to solve the queries received ` `void` `solve_queries(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `type, ql, qr, x, y; ` ` ` ` ` `cin >> type; ` ` ` ` ` `// If the query is of type 1. ` ` ` `if` `(type == 1) { ` ` ` ` ` `cin >> ql >> qr >> x; ` ` ` ` ` `// Counting the number of ` ` ` `// zeros in the given value of x ` ` ` `int` `temp = returnTwos(x); ` ` ` `int` `temp1 = returnFives(x); ` ` ` ` ` `for` `(` `int` `i = ql - 1; i < qr; i++) { ` ` ` ` ` `// The value x has been multiplied ` ` ` `// to their respective indices ` ` ` `arr[i] = arr[i] * x; ` ` ` ` ` `// The value obtained above has been ` ` ` `// added to their respective vectors ` ` ` `twos[i] += temp; ` ` ` `fives[i] += temp1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If the query is of type 2. ` ` ` `if` `(type == 2) { ` ` ` ` ` `cin >> ql >> qr >> y; ` ` ` ` ` `// Counting the number of ` ` ` `// zero in the given value of x ` ` ` `int` `temp = returnTwos(y); ` ` ` `int` `temp1 = returnFives(y); ` ` ` ` ` `for` `(` `int` `i = ql - 1; i < qr; i++) { ` ` ` ` ` `// The value y has been replaced ` ` ` `// to their respective indices ` ` ` `arr[i] = (i - ql + 2) * y; ` ` ` ` ` `// The value obtained above has been ` ` ` `// added to their respective vectors ` ` ` `twos[i] = returnTwos(i - ql + 2) + temp; ` ` ` `fives[i] = returnFives(i - ql + 2) + temp1; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If the query is of type 2 ` ` ` `if` `(type == 3) { ` ` ` ` ` `cin >> ql >> qr; ` ` ` `int` `sumtwos = 0; ` ` ` `int` `sumfives = 0; ` ` ` ` ` `for` `(` `int` `i = ql - 1; i < qr; i++) { ` ` ` ` ` `// as the number of trailing zeros for ` ` ` `// each case has been found for each array ` ` ` `// element then we simply add those to ` ` ` `// the respective index to a variable ` ` ` `sumtwos += twos[i]; ` ` ` `sumfives += fives[i]; ` ` ` `} ` ` ` ` ` `// Compare the number of zeros ` ` ` `// obtained in the multiples of five and two ` ` ` `// consider the minimum of them and add them ` ` ` `sum += min(sumtwos, sumfives); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n, m; ` ` ` ` ` `// Input the Size of array ` ` ` `// and number of queries ` ` ` `cin >> n >> m; ` ` ` ` ` `int` `arr[n]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `cin >> arr[i]; ` ` ` `twos[i] = returnTwos(arr[i]); ` ` ` `fives[i] = returnFives(arr[i]); ` ` ` `} ` ` ` ` ` `// Running the while loop ` ` ` `// for m number of queries ` ` ` `while` `(m--) { ` ` ` ` ` `solve_queries(arr, n); ` ` ` `} ` ` ` ` ` `cout << sum << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Input:

5 5 2 4 3 5 5 3 2 4 3 2 5 2 2 4 1 1 3 3 10 3 1 5

Output:

5

The complexity of this code is O(n).

This article is contributed by **Mohak Agrawal**. 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Queries to multiply the given subarray with given number X and print sum
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Minimize difference between maximum and minimum of Array by at most K replacements
- Minimum replacements to make elements of a ternary array same
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Array range queries over range queries
- Maximize product of array by replacing array elements with its sum or product with element from another array
- Range product queries in an array
- Count of divisors of product of an Array in range L to R for Q queries
- Queries for Count of divisors of product of an Array in given range | Set 2 (MO's Algorithm)
- Queries to find maximum product pair in range with updates
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Product of proper divisors of a number for Q queries
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Queries to search for an element in an array and modify the array based on given conditions
- Maximum Product Subarray | Added negative product case
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Find the Initial Array from given array after range sum queries