📜  具有最大算术平均值的最长子数组的长度。

📅  最后修改于: 2021-10-26 06:49:08             🧑  作者: Mango

给定一个由 n 个元素组成的数组,找到具有最大算术平均值的最长子数组。子数组的长度必须大于 1,且均值应仅作为整数计算。
例子:

Input : arr[] = {3, 2, 1, 2}
Output : 2
sub-array 3, 2 has greatest arithmetic mean

Input :arr[] = {3, 3, 3, 2}
Output : 3

这个想法是首先从数组中找到两个连续元素的最大平均值。再次迭代数组并尝试找到最长的序列,其中每个元素必须大于或等于计算的最大平均值。
由于以下关键点,上述方法有效:

  • 最小可能的序列长度为 2,因此两个连续元素的最大平均值将始终是结果的一部分。
  • 任何等于或大于计算平均值的元素都可以是最长序列的一部分。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find maximum distance
// between unequal elements
int longestSubarray(int arr[], int n)
{
 
    // Calculate maxMean
    int maxMean = 0;
    for (int i = 1; i < n; i++)
        maxMean = max(maxMean,
                      (arr[i] + arr[i - 1]) / 2);
 
    // Iterate over array and calculate largest subarray
    // with all elements greater or equal to maxMean
    int ans = 0;
    int subarrayLength = 0;
    for (int i = 0; i < n; i++)
        if (arr[i] >= maxMean)
            ans = max(ans, ++subarrayLength);
        else
            subarrayLength = 0;
 
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 3, 3, 2, 1, 4 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << longestSubarray(arr, n);
 
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG
{
     
// Function to find maximum distance
// between unequal elements
static int longestSubarray(int arr[], int n)
{
 
    // Calculate maxMean
    int maxMean = 0;
    for (int i = 1; i < n; i++)
        maxMean = Math.max(maxMean,
                    (arr[i] + arr[i - 1]) / 2);
 
    // Iterate over array and calculate largest subarray
    // with all elements greater or equal to maxMean
    int ans = 0;
    int subarrayLength = 0;
    for (int i = 0; i < n; i++)
        if (arr[i] >= maxMean)
            ans = Math.max(ans, ++subarrayLength);
        else
            subarrayLength = 0;
 
    return ans;
}
 
// Driver code
public static void main (String[] args)
{
 
    int arr[] = { 4, 3, 3, 2, 1, 4 };
    int n = arr.length;
    System.out.println (longestSubarray(arr, n));
}
}
 
// This code is contributed by ajit_00023


Python3
# Python implementation of the above approach
 
# Function to find maximum distance
# between unequal elements
def longestSubarray(arr, n):
 
    # Calculate maxMean
    maxMean = 0;
    for i in range(1, n):
        maxMean = max(maxMean,
                    (arr[i] + arr[i - 1]) // 2);
 
    # Iterate over array and calculate largest subarray
    # with all elements greater or equal to maxMean
    ans = 0;
    subarrayLength = 0;
    for i in range(n):
        if (arr[i] >= maxMean):
            subarrayLength += 1;
            ans = max(ans, subarrayLength);
        else:
            subarrayLength = 0;
 
    return ans;
 
# Driver code
arr = [ 4, 3, 3, 2, 1, 4 ];
 
n = len(arr);
 
print(longestSubarray(arr, n));
 
# This code contributed by PrinciRaj1992


C#
// C# program for the above approach
using System;
 
class GFG
{
     
    // Function to find maximum distance
    // between unequal elements
    static int longestSubarray(int []arr,
                               int n)
    {
     
        // Calculate maxMean
        int maxMean = 0;
        for (int i = 1; i < n; i++)
            maxMean = Math.Max(maxMean,
                              (arr[i] + arr[i - 1]) / 2);
     
        // Iterate over array and calculate
        // largest subarray with all elements
        // greater or equal to maxMean
        int ans = 0;
        int subarrayLength = 0;
        for (int i = 0; i < n; i++)
            if (arr[i] >= maxMean)
                ans = Math.Max(ans, ++subarrayLength);
            else
                subarrayLength = 0;
     
        return ans;
    }
     
    // Driver code
    public static void Main ()
    {
     
        int []arr = { 4, 3, 3, 2, 1, 4 };
        int n = arr.Length;
        Console.WriteLine(longestSubarray(arr, n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出:

3

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程