Open In App

Difference between maximum and minimum average of all K-length contiguous subarrays

Given an array arr[] of size N and an integer K, the task is to print the difference between the maximum and minimum average of the contiguous subarrays of length K.

Examples:



Input: arr[ ] = {3, 8, 9, 15}, K = 2
Output: 6.5
Explanation:
All subarrays of length 2 are {3, 8}, {8, 9}, {9, 15} and their averages are (3+8)/2 = 5.5, (8+9)/2 = 8.5, and (9+15)/2 = 12.0 respectively. 
Therefore, the difference between the maximum(=12.0) and minimum(=5.5) is 12.0 -5.5 = 6.5.

Input: arr[] = {17, 6.2, 19, 3.4}, K = 3
Output: 4.533



Naive Approach: The simplest approach is to find the average of every contiguous subarray of size K, and then find the maximum and minimum of these values, and print their difference.

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the difference between
// averages of the maximum and the minimum
// subarrays of length k
double Avgdifference(double arr[], int N, int K)
{
 
    // Stores min and max sum
    double min = 1000000, max = -1;
 
    // Iterate through starting points
    for (int i = 0; i <= N - K; i++) {
        double sum = 0;
 
        // Sum up next K elements
        for (int j = 0; j < K; j++) {
            sum += arr[i + j];
        }
 
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
 
    // Return the difference between max
    // and min average
    return (max - min) / K;
}
 
// Driver Code
int main()
{
    // Given Input
    double arr[] = { 3, 8, 9, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
 
    // Function Call
    cout << Avgdifference(arr, N, K);
 
    return 0;
}




// Java implementation of the above approach
import java.io.*;
 
class GFG
{
 
  // Function to find the difference between
  // averages of the maximum and the minimum
  // subarrays of length k
  static double Avgdifference(double arr[], int N, int K)
  {
 
    // Stores min and max sum
    double min = 1000000, max = -1;
 
    // Iterate through starting points
    for (int i = 0; i <= N - K; i++) {
      double sum = 0;
 
      // Sum up next K elements
      for (int j = 0; j < K; j++) {
        sum += arr[i + j];
      }
 
      // Update max and min moving sum
      if (min > sum)
        min = sum;
      if (max < sum)
        max = sum;
    }
 
    // Return the difference between max
    // and min average
    return (max - min) / K;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given Input
    double arr[] = { 3, 8, 9, 15 };
    int N =arr.length;
    int K = 2;
 
    // Function Call
 
    System.out.println( Avgdifference(arr, N, K));
  }
}
 
// This code is contributed by Potta Lokesh




# Python program for the above approach
 
# Function to find the difference between
# averages of the maximum and the minimum
# subarrays of length k
def Avgdifference(arr, N, K):
 
    # Stores min and max sum
    min = 1000000;
    max = -1;
 
    # Iterate through starting points
    for i in range(N - K + 1):
        sum = 0;
 
        # Sum up next K elements
        for j in range(K):
            sum += arr[i + j];
 
        # Update max and min moving sum
        if (min > sum):
            min = sum;
        if (max < sum):
            max = sum;
     
 
    # Return the difference between max
    # and min average
    return (max - min) / K;
 
 
# Driver Code
 
# Given Input
arr = [3, 8, 9, 15];
N = len(arr);
K = 2;
 
# Function Call
print(Avgdifference(arr, N, K));
 
# This code is contributed by _saurabh_jaiswal.




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the difference between
// averages of the maximum and the minimum
// subarrays of length k
static double Avgdifference(double []arr, int N, int K)
{
     
    // Stores min and max sum
    double min = 1000000, max = -1;
     
    // Iterate through starting points
    for(int i = 0; i <= N - K; i++)
    {
        double sum = 0;
         
        // Sum up next K elements
        for(int j = 0; j < K; j++)
        {
            sum += arr[i + j];
        }
     
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
     
    // Return the difference between max
    // and min average
    return(max - min) / K;
}
 
// Driver Code
public static void Main (String[] args)
{
     
    // Given Input
    double []arr = { 3, 8, 9, 15 };
    int N = arr.Length;
    int K = 2;
     
    // Function Call
    Console.Write(Avgdifference(arr, N, K));
}
}
 
// This code is contributed by shivanisinghss2110




<script>
 
// JavaScript program for the above approach
 
 
// Function to find the difference between
// averages of the maximum and the minimum
// subarrays of length k
function Avgdifference(arr, N, K) {
 
    // Stores min and max sum
    let min = 1000000, max = -1;
 
    // Iterate through starting points
    for (let i = 0; i <= N - K; i++) {
        let sum = 0;
 
        // Sum up next K elements
        for (let j = 0; j < K; j++) {
            sum += arr[i + j];
        }
 
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
 
    // Return the difference between max
    // and min average
    return (max - min) / K;
}
 
// Driver Code
 
// Given Input
let arr = [3, 8, 9, 15];
let N = arr.length;
let K = 2;
 
// Function Call
document.write(Avgdifference(arr, N, K));
 
</script>

Output
6.5

Time Complexity: O(N*K)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized using the sliding window technique. Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the difference between
// the maximum and minimum subarrays of
// length K
double Avgdifference(double arr[], int N, int K)
{
 
    // Stores the sum of subarray over the
    // range [0, K]
    double sum = 0;
    // Iterate over the range [0, K]
    for (int i = 0; i < K; i++)
        sum += arr[i];
 
    // Store min and max sum
    double min = sum;
    double max = sum;
 
    // Iterate over the range [K, N-K]
    for (int i = K; i <= N - K + 1; i++) {
 
        // Increment sum by arr[i]-arr[i-K]
        sum += arr[i] - arr[i - K];
 
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
 
    // Return difference between max and min
    // average
    return (max - min) / K;
}
// Driver Code
int main()
{
    // Given Input
    double arr[] = { 3, 8, 9, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
 
    // Function Call
    cout << Avgdifference(arr, N, K);
 
    return 0;
}




// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the difference between
// the maximum and minimum subarrays of
// length K
static double Avgdifference(double arr[], int N, int K)
{
     
    // Stores the sum of subarray over the
    // range [0, K]
    double sum = 0;
     
    // Iterate over the range [0, K]
    for(int i = 0; i < K; i++)
        sum += arr[i];
 
    // Store min and max sum
    double min = sum;
    double max = sum;
 
    // Iterate over the range [K, N-K]
    for(int i = K; i <= N - K + 1; i++)
    {
         
        // Increment sum by arr[i]-arr[i-K]
        sum += arr[i] - arr[i - K];
 
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
 
    // Return difference between max and min
    // average
    return(max - min) / K;
}
 
// Driver Code
public static void main (String[] args)
{
     
    // Given Input
    double arr[] = { 3, 8, 9, 15 };
    int N = arr.length;
    int K = 2;
     
    // Function Call
    System.out.println(Avgdifference(arr, N, K));
}
}
 
// This code is contributed by shivanisinghss2110




# python 3 program for the above approach
 
# Function to find the difference between
# the maximum and minimum subarrays of
# length K
def Avgdifference(arr, N, K):
   
    # Stores the sum of subarray over the
    # range [0, K]
    sum = 0
    # Iterate over the range [0, K]
    for i in range(K):
        sum += arr[i]
 
    # Store min and max sum
    min = sum
    max = sum
 
    # Iterate over the range [K, N-K]
    for i in range(K,N - K + 2,1):
       
        # Increment sum by arr[i]-arr[i-K]
        sum += arr[i] - arr[i - K]
 
        # Update max and min moving sum
        if (min > sum):
            min = sum
        if (max < sum):
            max = sum
 
    # Return difference between max and min
    # average
    return (max - min) / K
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    arr = [3, 8, 9, 15]
    N = len(arr)
    K = 2
 
    # Function Call
    print(Avgdifference(arr, N, K))
     
    # This code is contributed by ipg2016107.




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the difference between
// the maximum and minimum subarrays of
// length K
static double Avgdifference(double []arr, int N, int K)
{
     
    // Stores the sum of subarray over the
    // range [0, K]
    double sum = 0;
     
    // Iterate over the range [0, K]
    for(int i = 0; i < K; i++)
        sum += arr[i];
 
    // Store min and max sum
    double min = sum;
    double max = sum;
 
    // Iterate over the range [K, N-K]
    for(int i = K; i <= N - K + 1; i++)
    {
         
        // Increment sum by arr[i]-arr[i-K]
        sum += arr[i] - arr[i - K];
 
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
 
    // Return difference between max and min
    // average
    return(max - min) / K;
}
 
// Driver Code
public static void Main (String[] args)
{
     
    // Given Input
    double []arr = { 3, 8, 9, 15 };
    int N = arr.Length;
    int K = 2;
     
    // Function Call
    Console.Write(Avgdifference(arr, N, K));
}
}
 
// This code is contributed by shivanisinghss2110




<script>
// JavaScript program for the above approach
// Function to find the difference between
// the maximum and minimum subarrays of
// length K
function Avgdifference(arr, N, K)
{
     
    // Stores the sum of subarray over the
    // range [0, K]
    let sum = 0;
     
    // Iterate over the range [0, K]
    for(let i = 0; i < K; i++)
        sum += arr[i];
 
    // Store min and max sum
    let min = sum;
    let max = sum;
 
    // Iterate over the range [K, N-K]
    for(let i = K; i <= N - K + 1; i++)
    {
         
        // Increment sum by arr[i]-arr[i-K]
        sum += arr[i] - arr[i - K];
 
        // Update max and min moving sum
        if (min > sum)
            min = sum;
        if (max < sum)
            max = sum;
    }
 
    // Return difference between max and min
    // average
    return(max - min) / K;
}
 
// Driver Code
 
    // Given Input
    let arr = [ 3, 8, 9, 15 ];
    let N = arr.length;
    let K = 2;
     
    // Function Call
    document.write(Avgdifference(arr, N, K));
 
// This code is contributed by shivanisinghss2110
</script>

Output
6.5

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :