Products of ranges in an array | Product in range Queries in O ( N ) ; 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
import java . io . * ; class GFG { static int calculateProduct ( int [ ] A , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans = 1 ; for ( int i = L ; i <= R ; i ++ ) { ans = ans * A [ i ] ; ans = ans % P ; } return ans ; } static public void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 229 ; int L = 2 , R = 5 ; System . out . println ( calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; System . out . println ( calculateProduct ( A , L , R , P ) ) ; } }