Java Program for Find the subarray with least average Last Updated : 29 Dec, 2021 Comments Improve Suggest changes Like Article Like Report 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: 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 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 index 2) Find sum of first k elements. Let this sum be 'curr_sum' 3) Initialize min_sum = sum 4) 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. Java // A Simple Java program to find // minimum average subarray class Test { static int arr[] = new int[] { 3, 7, 90, 20, 10, 50, 40 }; // Prints beginning and ending indexes of subarray // of size k with minimum average static void findMinAvgSubarray(int n, int k) { // k must be smaller than or equal to n if (n < k) return; // Initialize beginning index of result int res_index = 0; // Compute sum of first subarray of size k int curr_sum = 0; for (int i = 0; i < k; i++) curr_sum += arr[i]; // Initialize minimum sum as current sum int min_sum = curr_sum; // Traverse from (k+1)'th element to n'th element for (int 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); } } System.out.println("Subarray between [" + res_index + ", " + (res_index + k - 1) + "] has minimum average"); } // Driver method to test the above function public static void main(String[] args) { int k = 3; // Subarray size findMinAvgSubarray(arr.length, k); } } Output: Subarray between [3, 5] has minimum average Time 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 Java Program for Find the subarray with least average kartik Follow Improve Article Tags : Java Mathematical Java Programs DSA Arrays Amazon +2 More Practice Tags : AmazonArraysJavaMathematical Similar Reads Java Program For Finding Subarray With Given Sum - Set 1 (Nonnegative Numbers) Given an unsorted array of nonnegative integers, find a continuous subarray which adds to a given number. Examples : Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33 Output: Sum found between indexes 2 and 4 Sum of elements between indices 2 and 4 is 20 + 3 + 10 = 33 Input: arr[] = {1, 4, 0, 0, 3, 10, 5 min read Java 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 4 min read Java Program to Print the Smallest Element in an Array Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value 3 min read Java 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}; 4 min read Java Program to Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m = 5 min read Java Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9 Output : Median is 5 If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9) Input: 1 3 4 2 5 6 7 8 9 Output: Media 4 min read Java Program for Queries to find maximum sum contiguous subarrays of given length in a rotating array Given an array arr[] of N integers and Q queries of the form {X, Y} of the following two types: If X = 1, rotate the given array to the left by Y positions.If X = 2, print the maximum sum subarray of length Y in the current state of the array. Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, 5 min read Java Program for Equilibrium index of an array Equilibrium index of an array is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in an array A: Example : Input: A[] = {-7, 1, 5, 2, -4, 3, 0} Output: 3 3 is an equilibrium index, because: A[0] + A[1] + A[2] = A[4] + A[5] + A[6] 5 min read Calculate the Sum and Average of Elements in an ArrayList in Java A Dynamic and Adaptable method for storing and managing collections of elements is to use ArrayList. Finding the total and average of an ArrayList's items is frequently required when working with numerical data that is stored in the list. In this article, we will see how we can sum and find the aver 3 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 Like