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