Open In App

Array Queries for multiply, replacements and product

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.

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. 



Implementation:





















Output
0

Time complexity: O(n*qlogn).
Auxiliary space: O(k) where k=1000.

For each query, it is taking O(nlogn) so the final time complexity is O(n*q)

 


Article Tags :