PHP Program for Products of ranges in an array Last Updated : 22 Jul, 2024 Comments Improve Suggest changes 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 : Web Technologies PHP PHP Programs DSA Arrays array-range-queries Modular Arithmetic +3 More Practice Tags : ArraysModular Arithmetic Similar Reads PHP Program for Minimum Product Subset of an Array Given an array Arr, we have to find the minimum product possible with the subset of elements present in the array. The minimum product can be a single element also.Examples: Input : Arr = [ -1, -1, -2, 4, 3 ] Output : -24 Explanation : Minimum product will be ( -2 * -1 * -1 * 4 * 3 ) = -24 Input : A 3 min read PHP Program for Mean of range in array Given an array of n integers. You are given q queries. Write a program to print the floor value of mean in range l to r for each query in a new line.Examples : Input : arr[] = {1, 2, 3, 4, 5} q = 3 0 2 1 3 0 4Output : 2 3 3Here for 0 to 2 (1 + 2 + 3) / 3 = 2Input : arr[] = {6, 7, 8, 10} q = 2 0 3 1 2 min read PHP Program for Ceiling in a sorted array Given a sorted array and a value x, the ceiling of x is the smallest element in array greater than or equal to x, and the floor is the greatest element smaller than or equal to x. Assume than the array is sorted in non-decreasing order. Write efficient functions to find floor and ceiling of x. Examp 4 min read PHP Program to Print all triplets in sorted array that form AP Given a sorted array of distinct positive integers, print all triplets that form AP (or Arithmetic Progression)Examples : Input : arr[] = { 2, 6, 9, 12, 17, 22, 31, 32, 35, 42 };Output :6 9 122 12 2212 17 222 17 3212 22 329 22 352 22 4222 32 42Input : arr[] = { 3, 5, 6, 7, 8, 10, 12};Output :3 5 75 4 min read How to print all the values of an array in PHP ? We have given an array containing some array elements and the task is to print all the values of an array arr in PHP. In order to do this task, we have the following approaches in PHP:Table of ContentUsing for-each loop: Using count() function and for loop: Using implode():Using print_r() FunctionUs 3 min read Like