Products of ranges in an array | Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code
function calculateProduct ( A , L , R , P ) { L = L - 1 ; R = R - 1 ; let ans = 1 ; for ( let i = L ; i <= R ; i ++ ) { ans = ans * A [ i ] ; ans = ans % P ; } return ans ; } let A = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; let P = 229 ; let L = 2 , R = 5 ; document . write ( calculateProduct ( A , L , R , P ) + " " ) ; L = 1 ; R = 3 ; document . write ( calculateProduct ( A , L , R , P ) + " " ) ;