Maximum Average Sub-array of K length in Java

Maximum Average Sub-array of K length in Java

On this page we will discuss about Maximum Average sub-array of k length in Java language . We have to Find out the maximum possible average value of sub-array of K length from  given sequence of N integers, a[1], a[2], , , , a[N] of  N length and a integer K integer.
Average sub-array

Maximum Average Sub-array of K length in Java

In Java, maximum average subarray of k length pertains to a contiguous sub-array of length k in a given array of numbers, where the average (mean) of the k elements is the highest among all possible sub-arrays of length k in that array. In simpler words, it refers to the sub-array of k consecutive elements whose sum is the largest possible among all sub-arrays of k consecutive elements in the array, resulting in the highest average value.

For example,consider the array

[1, 12, -5, -6, 50, 3] and k=4.

The subarrays of length 4 are [1, 12, -5, -6], [12, -5, -6, 50], [-5, -6, 50, 3], and their averages are 0.5, 12.75, and 10.5 respectively. The maximum average subarray of length 4 in this case is [12, -5, -6, 50], whose average is 12.75.

Maximum Average Sub-array of K length in Java

Algorithm:

  1. Initialize max_sum with the sum of the first k elements of arr and max_end with k-1, which represent the sum and ending index of the first subarray of length k.

  2. Loop through the input array arr from index k to n-1 and for each index i, compute the sum of the subarray of length k ending at index i, i.e., curr_sum = sum of elements from arr[i-k+1] to arr[i].

  3. Compare curr_sum with max_sum. If curr_sum is greater than max_sum, update max_sum with curr_sum and update max_end with the current index i.

  4. After the loop, max_end will represent the ending index of the maximum average subarray of length k.

  5. Return the starting index of the maximum average subarray of length k as max_end - k + 1.

Java code for maximum average sub-array of k length

Run
import java.util.Arrays;

public class Main {
    // Returns beginning index of maximum average
    // subarray of length 'k'
    static int findMaxAverage(int[] arr, int n, int k) {
        // Check if 'k' is valid
        if (k > n) {
            return -1;
        }

        // Create and fill array to store cumulative
        // sum. csum[i] stores sum of arr[0] to arr[i]
        int[] csum = new int[n];
        csum[0] = arr[0];
        for (int i = 1; i < n; i++) {
            csum[i] = csum[i - 1] + arr[i];
        }

        int max_sum = csum[k - 1], max_end = k - 1;
        for (int i = k; i < n; i++) { int curr_sum = csum[i] - csum[i - k]; if (curr_sum > max_sum) {
                max_sum = curr_sum;
                max_end = i;
            }
        }

        // Return starting index
        return max_end - k + 1;
    }

    // Driver program
    public static void main(String[] args) {
        int[] arr = {-1, 10, -15, -6, 50, 3};
        int k = 4;
        int n = arr.length;
        System.out.println("The maximum average subarray of length " + k + " begins at index " + findMaxAverage(arr, n, k));
    }
}

Output

The maximum average subarray of length 4 begins at index 1

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java