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 // 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 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 PHP Program to Count Primes in Ranges Given a range [L, R], we need to find the count of total numbers of prime numbers in the range [L, R] where 0 <= L <= R < 10000. Consider that there are a large number of queries for different ranges.Examples: Input : Query 1 : L = 1, R = 10 Query 2 : L = 5, R = 10 Output : 4 2 Explanation: 3 min read PHP Program to Print All Even Numbers in a Range Given two numbers, Start and End, the task is to print all even numbers within a given range using PHP. Even numbers are integers that are divisible by 2, meaning they have no remainder when divided by 2. Examples: Input: start = 1, end = 10Output: 2 4 6 8 10Input: start = 21, end = 35Output: 22 24 3 min read PHP Program for Range Queries for Frequencies of Array Elements Given an array of n non-negative integers. The task is to find the frequency of a particular element in the arbitrary range of array[]. The range is given as positions (not 0 based indexes) in array. There can be multiple queries of given type. Examples: Input : arr[] = {2, 8, 6, 9, 8, 6, 8, 2, 11}; 2 min read How to Push Both Key and Value into PHP Array ? Given an array, the task is to push a new key and value pair into the array. There are some methods to push value and key into the PHP array, these are:Table of ContentUsing Square Bracket [] SyntaxUsing array_merge() FunctionUsing += OperatorUsing the array_combine FunctionUsing array_replace()Usin 4 min read How to re-index an array in PHP? The re-index of an array can be done by using some inbuilt function together. These functions are: array_combine() Function: The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. 5 min read Like