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 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 Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples:Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input : arr[] = {2, 5, 6, 7, 8, 8, 9}; 3 min read PHP Program for Maximum Equilibrium Sum in an Array Given an array arr[]. Find the maximum value of prefix sum which is also suffix sum for index i in arr[].Examples: Input : arr[] = {-1, 2, 3, 0, 3, 2, -1}Output : 4Prefix sum of arr[0..3] = Suffix sum of arr[3..6]Input : arr[] = {-2, 5, 3, 1, 2, 6, -4, 2}Output : 7Prefix sum of arr[0..3] = Suffix su 3 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 Counting Inversions In An Array - Set 1 (Using Merge Sort) Inversion Count for an array indicates - how far (or close) the array is from being sorted. If the array is already sorted, then the inversion count is 0, but if the array is sorted in the reverse order, the inversion count is the maximum. Formally speaking, two elements a[i] and a[j] form an invers 2 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 PHP Program to Delete Middle Element from an Array Given an array, the task is to delete the middle element of the array. If the array has an even number of elements, the middle element can be considered as the one closer to the start of the array (the lower middle).These are the following approaches:Table of ContentUsing array_splice() functionUsin 3 min read PHP Program to Find Sum of All Matrix Elements Finding the sum of all elements in a matrix is a common operation in mathematical computations and programming. In PHP, this can be achieved using various approaches, including loops and array functions. In this article, we will explore different methods to calculate the sum of all elements in a mat 4 min read Like