Multiplication on Array : Range update query in O ( 1 ) | Creates a mul array for A and returns it after filling initial values . ; Does range update ; Prints updated Array ; Array to be updated ; Create and fill mul and div Array
function initialize ( mul , div , size ) { for ( i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } function update ( l , r , x , mul , div ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } function printArray ( ar , mul , div , n ) { for ( i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; document . write ( ar [ i ] + " " ) ; } } var ar = [ 10 , 5 , 20 , 40 ] ; var n = ar . length ; var mul = Array ( n + 1 ) . fill ( 0 ) ; var div = Array ( n + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ;