📌  相关文章
📜  每个Array元素最大的最长子数组

📅  最后修改于: 2021-05-17 03:10:21             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是为每个数组元素arr [i]找到最长的子数组,该数组包含arr [i]作为最大值。

例子:

方法:想法是使用两指针技术解决问题:

  • 初始化两个指针, leftright 。这样,对于每个元素arr [i]左侧都指向索引[i – 1,0 ],以连续的方式找到小于或等于arr [i]的元素。一旦找到一个大于arr [i]的元素,指针就会停止。
  • 同样,指向索引[i + 1,n – 1] ,以连续的方式查找小于或等于arr [i]的元素,并停止查找大于arr [i]的元素。
  • 因此, arr [i]最大的最大连续子数组的长度为1 +右–左
  • 对每个数组元素重复上述步骤。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
void solve(int n, int arr[])
{
    int i, ans = 0;
    for (i = 0; i < n; i++) {
  
        // Initialize the bounds
        int left = max(i - 1, 0);
        int right = min(n - 1, i + 1);
  
        // Iterate to find greater
        // element on the left
        while (left >= 0) {
  
            // If greater element is found
            if (arr[left] > arr[i]) {
                left++;
                break;
            }
  
            // Decrement left pointer
            left--;
        }
  
        // If boundary is exceeded
        if (left < 0)
            left++;
  
        // Iterate to find greater
        // element on the right
        while (right < n) {
  
            // If greater element is found
            if (arr[right] > arr[i]) {
                right--;
                break;
            }
            // Increment right pointer
            right++;
        }
  
        // If boundary is exceeded
        if (right >= n)
            right--;
  
        // Length of longest subarray where
        // arr[i] is the largest
        ans = 1 + right - left;
  
        // Print the answer
        cout << ans << " ";
    }
}
  
// Driver Code
int main()
{
    int arr[] = { 4, 2, 1 };
    int n = sizeof arr / sizeof arr[0];
  
    solve(n, arr);
  
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
  
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
static void solve(int n, int arr[])
{
    int i, ans = 0;
    for (i = 0; i < n; i++) 
    {
  
        // Initialize the bounds
        int left = Math.max(i - 1, 0);
        int right = Math.min(n - 1, i + 1);
  
        // Iterate to find greater
        // element on the left
        while (left >= 0) 
        {
  
            // If greater element is found
            if (arr[left] > arr[i]) 
            {
                left++;
                break;
            }
  
            // Decrement left pointer
            left--;
        }
  
        // If boundary is exceeded
        if (left < 0)
            left++;
  
        // Iterate to find greater
        // element on the right
        while (right < n)
        {
  
            // If greater element is found
            if (arr[right] > arr[i]) 
            {
                right--;
                break;
            }
            
            // Increment right pointer
            right++;
        }
  
        // If boundary is exceeded
        if (right >= n)
            right--;
  
        // Length of longest subarray where
        // arr[i] is the largest
        ans = 1 + right - left;
  
        // Print the answer
        System.out.print(ans + " ");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 4, 2, 1 };
    int n = arr.length;
  
    solve(n, arr);
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
  
# Function to find the maximum length of
# Subarrays for each element of the array
# having it as the maximum
def solve(n, arr):
      
    ans = 0
      
    for i in range(n):
          
        # Inititalise the bounds
        left = max(i - 1, 0)
        right = min(n - 1, i + 1)
          
        # Iterate to find greater
        # element on the left
        while left >= 0:
              
            # If greater element is found
            if arr[left] > arr[i]:
                left += 1
                break
                  
            # Decrement left pointer
            left -= 1
              
        # If boundary is exceeded
        if left < 0:
            left += 1
              
        # Iterate to find greater
        # element on the right
        while right < n:
              
            # If greater element is found
            if arr[right] > arr[i]:
                right -= 1
                break
              
            # Increment right pointer
            right += 1
          
        # if boundary is exceeded 
        if right >= n:
            right -= 1
              
        # Length of longest subarray where
        # arr[i] is the largest
        ans = 1 + right - left
          
        # Print the answer
        print(ans, end = " ")
          
# Driver code
arr = [ 4, 2, 1 ]
n = len(arr)
  
solve(n, arr)
      
# This code is contributed by Stuti Pathak


C#
// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
static void solve(int n, int []arr)
{
    int i, ans = 0;
    for (i = 0; i < n; i++) 
    {
  
        // Initialize the bounds
        int left = Math.Max(i - 1, 0);
        int right = Math.Min(n - 1, i + 1);
  
        // Iterate to find greater
        // element on the left
        while (left >= 0) 
        {
  
            // If greater element is found
            if (arr[left] > arr[i]) 
            {
                left++;
                break;
            }
  
            // Decrement left pointer
            left--;
        }
  
        // If boundary is exceeded
        if (left < 0)
            left++;
  
        // Iterate to find greater
        // element on the right
        while (right < n)
        {
  
            // If greater element is found
            if (arr[right] > arr[i]) 
            {
                right--;
                break;
            }
            
            // Increment right pointer
            right++;
        }
  
        // If boundary is exceeded
        if (right >= n)
            right--;
  
        // Length of longest subarray where
        // arr[i] is the largest
        ans = 1 + right - left;
  
        // Print the answer
        Console.Write(ans + " ");
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 4, 2, 1 };
    int n = arr.Length;
  
    solve(n, arr);
}
}
  
// This code is contributed by Princi Singh


输出:
3 2 1

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