PHP Program for Mean of range in array Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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 2Output : 7 7Naive Approach: We can run loop for each query l to r and find sum and number of elements in range. After this we can print floor of mean for each query. PHP <?php // PHP program to find floor // value of mean in range l to r // To find mean of // range in l to r function findMean($arr, $l, $r) { // Both sum and count // are initialize to 0 $sum = 0; $count = 0; // To calculate sum and // number of elements in // range l to r for ($i = $l; $i <= $r; $i++) { $sum += $arr[$i]; $count++; } // Calculate floor // value of mean $mean = floor($sum / $count); // Returns mean of array // in range l to r return $mean; } // Driver Code $arr = array(1, 2, 3, 4, 5); echo findMean($arr, 0, 2), " "; echo findMean($arr, 1, 3), " "; echo findMean($arr, 0, 4), " "; // This code is contributed by ajit ?> Output2 3 3 Time Complexity: O(n). Please refer complete article on Mean of range in array for more details! Comment More infoAdvertise with us Next Article PHP Program for Mean of range in array K kartik Follow Improve Article Tags : Web Technologies PHP PHP Programs DSA Arrays array-range-queries prefix-sum +3 More Practice Tags : Arraysprefix-sum Similar Reads 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 PHP Array Programs PHP Arrays is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. S. No Articles 1PHP Program to Insert a New Element in an Array2PHP Program to Find the Index of 4 min read PHP Program for Find the Subarray with Least Average Given an array Arr of size n and integer k such that k <= n, the task is to find the subarray with least average.Examples:Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3Output: Subarray between indexes 3 and 5The subarray {20, 10, 50} has the least average among all subarrays of size 3.Input: ar 3 min read PHP Program to Calculate Root Mean Square Root Mean Square (RMS) is a statistical measure of the magnitude of a varying quantity. It is commonly used in physics, engineering, and statistics to represent the effective value of a set of numbers. In this article, we will discuss how to calculate the RMS of a set of numbers in PHP using differe 2 min read How to Iterate Over an Array in PHP? Arrays are fundamental data structures in PHP, and the ability to iterate through them is crucial for manipulating and processing data. In this article, we will explore various approaches to iterate through arrays in PHP.Table of ContentUsing for LoopUsing foreach LoopUsing while Loop with each() Fu 3 min read Like