Array Queries for multiply , replacements and product | Java 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
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static Scanner sc = new Scanner ( System . in ) ; static int twos [ ] = new int [ 1000 ] ; static int fives [ ] = new int [ 1000 ] ; static int sum = 0 ; static int returnTwos ( int val ) { int count = 0 ; while ( val % 2 == 0 && val != 0 ) { val = val / 2 ; count ++ ; } return count ; } static int returnFives ( int val ) { int count = 0 ; while ( val % 5 == 0 && val != 0 ) { val = val / 5 ; count ++ ; } return count ; } static void solve_queries ( int arr [ ] , int n ) { int type = sc . nextInt ( ) ; if ( type == 1 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; 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 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int y = sc . nextInt ( ) ; 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 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int sumtwos = 0 ; int sumfives = 0 ; for ( int i = ql - 1 ; i < qr ; i ++ ) { sumtwos += twos [ i ] ; sumfives += fives [ i ] ; } sum += Math . min ( sumtwos , sumfives ) ; } } public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = sc . nextInt ( ) ; twos [ i ] = returnTwos ( arr [ i ] ) ; fives [ i ] = returnFives ( arr [ i ] ) ; } while ( m -- != 0 ) { solve_queries ( arr , n ) ; } System . out . println ( sum ) ; } }