📌  相关文章
📜  所有 K 长度连续子阵列的最大和最小平均值之间的差异

📅  最后修改于: 2022-05-13 01:56:06.198000             🧑  作者: Mango

所有 K 长度连续子阵列的最大和最小平均值之间的差异

给定一个大小为N的数组arr[]和一个整数K,任务是打印长度为K的连续子数组的最大和最小平均值之间的差。

例子:

朴素方法:最简单的方法是找到每个大小为K的连续子数组的平均值,然后找到这些值的最大值和最小值,并打印它们的差值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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
// 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


Python3
# 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#
// 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


Javascript


C++
// C++ program for the above approach
#include 
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
// 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


Python3
# 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#
// 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


Javascript


输出
6.5

时间复杂度: O(N*K)
辅助空间: O(1)

高效方法:上述方法可以使用滑动窗口技术进行优化。请按照以下步骤解决问题:

  • [0, K-1]范围内找到子数组的总和,并将其存储在变量sum中。
  • 初始化两个变量,比如maxmin ,以存储任何大小为K的子数组的最大和最小和。
  • 使用变量i迭代范围[K, N-K+1]并执行以下步骤:
    • 删除元素arr[iK]并将元素arr[i]添加到大小为K的窗口中。即将 sum 更新为sum +arr[i]-arr[iK]。
    • min更新为minsum 的最小值,并将max更新为maxsum的最大值。
  • 最后,完成上述步骤后,将答案打印为(max-min)/K。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
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

// 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

Python3

# 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#

// 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

Javascript


输出
6.5

时间复杂度: O(N)
辅助空间: O(1)