PHP Program for Mean of range in array Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share 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 : PHP array-range-queries prefix-sum Practice Tags : prefix-sum Similar Reads Javascript 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 4 Output : 2 3 3 Here for 0 to 2 (1 + 2 + 3) / 3 = 2 Input : arr[] = {6, 7, 8, 10} q = 2 0 3 3 min read Mean of range in array Given an array arr[] of n integers and q queries represented by an array queries[][], where queries[i][0] = l and queries[i][1] = r. For each query, the task is to calculate the mean of elements in the range l to r and return its floor value. Examples: Input: arr[] = [3, 7, 2, 8, 5] queries[][] = [[ 12 min read PHP Program To Find Mean and Median of an Unsorted Array Given an unsorted array, the task is to find the mean (average) and median of the array in PHP. we will see the approach and code example to solve this problem. ApproachTo find the mean of an array, we need to sum up all the elements in the array and then divide the sum by the total number of elemen 2 min read PHP Program for Median of two sorted arrays of same size Write a PHP program for a given 2 sorted arrays A and B of size n each. the task is to find the median of the array obtained by merging the above 2 arrays(i.e. array of length 2n). The complexity should be O(log(n)). Examples: Input: ar1[] = {1, 12, 15, 26, 38} ar2[] = {2, 13, 17, 30, 45}Output: 16E 6 min read PHP program to find the Standard Deviation of an array Given an array of elements. We need to find the Standard Deviation of the elements of the array in PHP. Examples: Input : array(2, 3, 5, 6, 7)Output : 1.5620499351813Input : array(1, 2, 3, 4, 5)Output : 1 The following problem can be solved using the PHP inbuilt functions. The inbuilt functions used 2 min read Like