PHP Program for Find the Subarray with Least Average Last Updated : 22 Jul, 2024 Summarize Comments Improve Suggest changes Share 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 K kartik Follow Improve Article Tags : PHP Amazon Practice Tags : Amazon Similar Reads Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Primâs Algorithm for Minimum Spanning Tree (MST) Primâs algorithm is a Greedy algorithm like Kruskal's algorithm. This algorithm always starts with a single node and moves through several adjacent nodes, in order to explore all of the connected edges along the way.The algorithm starts with an empty spanning tree. The idea is to maintain two sets o 15+ min read Maximum Subarray Sum - Kadane's Algorithm Given an integer array arr[], find the subarray (containing at least one element) which has the maximum possible sum, and return that sum.Note: A subarray is a continuous part of an array.Examples:Input: arr[] = [2, 3, -8, 7, -1, 2, 3]Output: 11Explanation: The subarray [7, -1, 2, 3] has the largest 8 min read Heap Sort - Data Structures and Algorithms Tutorials Heap sort is a comparison-based sorting technique based on Binary Heap Data Structure. It can be seen as an optimization over selection sort where we first find the max (or min) element and swap it with the last (or first). We repeat the same process for the remaining elements. In Heap Sort, we use 14 min read Topological Sorting Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u-v, vertex u comes before v in the ordering.Note: Topological Sorting for a graph is not possible if the graph is not a DAG.Example:Input: V = 6, edges = [[2, 3], [3, 1], [4, 0], 10 min read Array Reverse - Complete Tutorial Given an array arr[], the task is to reverse the array. Reversing an array means rearranging the elements such that the first element becomes the last, the second element becomes second last and so on.Examples: Input: arr[] = {1, 4, 3, 2, 6, 5} Output: {5, 6, 2, 3, 4, 1}Explanation: The first elemen 15+ min read Reverse a Linked List Given a linked list, the task is to reverse the linked list by changing the links between nodes.Examples: Input: head: 1 -> 2 -> 3 -> 4 -> NULLOutput: head: 4 -> 3 -> 2 -> 1 -> NULLExplanation: Reversed Linked List: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOut 15+ min read Two Sum - Pair with given Sum Given an array arr[] of n integers and a target value, check if there exists a pair whose sum equals the target. This is a variation of the 2Sum problem.Examples: Input: arr[] = [0, -1, 2, -3, 1], target = -2Output: trueExplanation: There is a pair (1, -3) with the sum equal to given target, 1 + (-3 15+ min read Infix to Postfix Expression Write a program to convert an Infix expression to Postfix form.Infix expression: The expression of the form "a operator b" (a + b) i.e., when an operator is in-between every pair of operands.Postfix expression: The expression of the form "a b operator" (ab+) i.e., When every pair of operands is foll 9 min read Like