PHP Program for Products of ranges in an array Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an array A[] of size N. Solve Q queries. Find the product in the range [L, R] under modulo P ( P is Prime). Examples: Input : A[] = {1, 2, 3, 4, 5, 6} L = 2, R = 5, P = 229Output : 120Input : A[] = {1, 2, 3, 4, 5, 6}, L = 2, R = 5, P = 113Output : 7 Brute Force ApproachFor each of the queries, traverse each element in the range [L, R] and calculate the product under modulo P. This will answer each query in O(N). PHP <?php // Product in range Queries in O(N) // Function to calculate // Product in the given range. function calculateProduct($A, $L, $R, $P) { // As our array is 0 based as // and L and R are given as 1 // based index. $L = $L - 1; $R = $R - 1; $ans = 1; for ($i = $L; $i <= $R; $i++) { $ans = $ans * $A[$i]; $ans = $ans % $P; } return $ans; } // Driver code $A = array( 1, 2, 3, 4, 5, 6 ); $P = 229; $L = 2; $R = 5; echo calculateProduct($A, $L, $R, $P)," " ; $L = 1; $R = 3; echo calculateProduct($A, $L, $R, $P)," " ; // This code is contributed by ajit. ?> Output120 6 Please refer complete article on Products of ranges in an array for more details! Comment More infoAdvertise with us Next Article PHP Program for Products of ranges in an array K kartik Follow Improve Article Tags : PHP array-range-queries Modular Arithmetic Practice Tags : Modular Arithmetic Similar Reads Javascript Program for Products of ranges in an array Given an array A[] of size N. Solve Q queries. Find the product in the range [L, R] under modulo P ( P is Prime). Examples: Input : A[] = {1, 2, 3, 4, 5, 6} L = 2, R = 5, P = 229Output : 120Input : A[] = {1, 2, 3, 4, 5, 6}, L = 2, R = 5, P = 113Output : 7 Brute Force Approach:For each of the queries 4 min read Products of ranges in an array Given an array A[] of size N. Solve Q queries. Find the product in the range [L, R] under modulo P ( P is Prime). Examples: Input : A[] = {1, 2, 3, 4, 5, 6} L = 2, R = 5, P = 229 Output : 120 Input : A[] = {1, 2, 3, 4, 5, 6}, L = 2, R = 5, P = 113 Output : 7 Brute Force: For each of the queries, tra 15 min read Range product queries in an array We have an array of integers and a set of range queries. For every query, we need to find product of elements in the given range. Example: Input : arr[] = {5, 10, 15, 20, 25} queries[] = {(3, 5), (2, 2), (2, 3)} Output : 7500, 10, 150 7500 = 15 x 20 x 25 10 = 10 150 = 10 x 15 A naive approach would 10 min read Array Queries for multiply, replacements and product This is Range query question in which we have been provided with and array of size N. Given are 3 types of queries and you have to answer M number of specified queries. Type 1 query :You will be given 3 values in the form of L R X and in this type of query to have to multiply x to the array elements 15+ min read How to Loop Through an Array using a foreach Loop in PHP? Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iteratin 2 min read Like