PHP Program for Find the Subarray with Least Average Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2Output: Subarray between [4, 5] has minimum averageWe strongly recommend that you click here and practice it, before moving on to the solution.A Simple Solution is to consider every element as a beginning of subarray of size k and compute sum of subarray starting with this element. Time complexity of this solution is O(nk).An Efficient Solution is to solve the above problem in O(n) time and O(1) extra space. The idea is to use sliding window of size k. Keep track of sum of current k elements. To compute sum of current window, remove first element of previous window and add current element (last element of current window).1) Initialize res_index = 0 // Beginning of result index2) Find sum of first k elements. Let this sum be 'curr_sum'3) Initialize min_sum = sum4) Iterate from (k+1)'th to n'th element, do following for every element arr[i] a) curr_sum = curr_sum + arr[i] - arr[i-k] b) If curr_sum < min_sum res_index = (i-k+1)5) Print res_index and res_index+k-1 as beginning and ending indexes of resultant subarray.Below is the implementation of above algorithm. PHP <?php // A Simple PHP program to find // minimum average subarray // Prints beginning and ending // indexes of subarray of size // k with minimum average function findMinAvgSubarray($arr, $n, $k) { // k must be smaller // than or equal to n if ($n < $k) return; // Initialize beginning // index of result $res_index = 0; // Compute sum of first // subarray of size k $curr_sum = 0; for ($i = 0; $i < $k; $i++) $curr_sum += $arr[$i]; // Initialize minimum sum // as current sum $min_sum = $curr_sum; // Traverse from (k+1)'th element // to n'th element for ( $i = $k; $i < $n; $i++) { // Add current item and // remove first item of // previous subarray $curr_sum += $arr[$i] - $arr[$i - $k]; // Update result if needed if ($curr_sum < $min_sum) { $min_sum = $curr_sum; $res_index = ($i - $k + 1); } } echo "Subarray between [" ,$res_index , ", " ,$res_index + $k - 1, "] has minimum average"; } // Driver Code $arr = array(3, 7, 90, 20, 10, 50, 40); // Subarray size $k = 3; $n = sizeof($arr); findMinAvgSubarray($arr, $n, $k); return 0; ?> OutputSubarray between [3, 5] has minimum averageTime Complexity: O(n) Auxiliary Space: O(1)Please refer complete article on Find the subarray with least average for more details! Comment More infoAdvertise with us Next Article PHP Program for Find the Subarray with Least Average kartik Follow Improve Article Tags : PHP Amazon Practice Tags : Amazon Similar Reads Javascript Program to Find the subarray with least average Given an array arr[] of size n and integer k such that k <= n.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: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2Output: Sub 3 min read Find the subarray with least average Given an array arr[] of size n and integer k such that k <= n. Examples : Input: arr[] = {3, 7, 90, 20, 10, 50, 40}, k = 3 Output: Subarray between indexes 3 and 5 The subarray {20, 10, 50} has the least average among all subarrays of size 3. Input: arr[] = {3, 7, 5, 20, -10, 0, 12}, k = 2 Output 12 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 Smallest subarray with sum greater than a given value Given an array arr[] of integers and a number x, the task is to find the smallest subarray with a sum strictly greater than x.Examples:Input: x = 51, arr[] = [1, 4, 45, 6, 0, 19]Output: 3Explanation: Minimum length subarray is [4, 45, 6]Input: x = 100, arr[] = [1, 10, 5, 2, 7]Output: 0Explanation: N 15+ min read PHP program to find the maximum and the minimum in array Finding the maximum and minimum in an array involves determining the largest and smallest values within a given set of numbers. This task is crucial for analyzing data, identifying trends, or filtering outliers. Various methods, from simple traversal to optimized algorithms, can achieve this.Example 7 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 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 Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera 3 min read Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1, 2 min read How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read Like