📌  相关文章
📜  在子数组的两半上由相同类型的元素组成的子数组的最大长度

📅  最后修改于: 2021-04-29 19:02:18             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是在子数组的两半上找到由相同类型的元素组成的子数组的最大长度。同样,两个半部的元素互不相同。

例子:

天真的方法:天真的想法是生成所有可能的子数组,并检查具有最大长度的任何子数组可分为两半,以使两半中的所有元素都相同。

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

高效的方法:解决此问题的想法是使用“前缀和”的概念。请按照以下步骤解决问题:

  1. 从头开始向前遍历数组,并将连续出现的整数连续存储在forward []数组中
  2. 同样,从末尾沿相反方向遍历数组,并将每个索引的连续出现连续存储在向后数组[]中
  3. 对于其中arr [i]!= arr [i + 1]的所有索引,存储min(forward [i],backward [i + 1])* 2的最大值。
  4. 打印在上述步骤中获得的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that finds the maximum
// length of the sub-array that
// contains equal element on both
// halves of sub-array
void maxLengthSubArray(int A[], int N)
{
 
    // To store continuous occurence
    // of the element
    int forward[N], backward[N];
 
    // To store continuous
    // forward occurence
    for (int i = 0; i < N; i++) {
 
        if (i == 0
            || A[i] != A[i - 1]) {
            forward[i] = 1;
        }
        else
            forward[i] = forward[i - 1] + 1;
    }
 
    // To store continuous
    // backward occurence
    for (int i = N - 1; i >= 0; i--) {
 
        if (i == N - 1
            || A[i] != A[i + 1]) {
            backward[i] = 1;
        }
        else
            backward[i] = backward[i + 1] + 1;
    }
 
    // To store the maximum length
    int ans = 0;
 
    // Find maximum length
    for (int i = 0; i < N - 1; i++) {
 
        if (A[i] != A[i + 1])
            ans = max(ans,
                    min(forward[i],
                        backward[i + 1])
                        * 2);
    }
 
    // Print the result
    cout << ans;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 2, 3, 4, 4,
                4, 6, 6, 6, 9 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    maxLengthSubArray(arr, N);
    return 0;
}


Java
// Java program for the above approach         
class GFG{         
            
// Function that finds the maximum         
// length of the sub-array that         
// contains equal element on both         
// halves of sub-array         
static void maxLengthSubArray(int A[], int N)         
{
     
    // To store continuous occurence         
    // of the element         
    int forward[] = new int[N];         
    int backward[] = new int[N];         
     
    // To store continuous         
    // forkward occurence         
    for(int i = 0; i < N; i++)
    {
        if (i == 0 || A[i] != A[i - 1])
        {         
            forward[i] = 1;         
        }         
        else         
            forward[i] = forward[i - 1] + 1;         
    }         
     
    // To store continuous         
    // backward occurence         
    for(int i = N - 1; i >= 0; i--)
    {
        if (i == N - 1 || A[i] != A[i + 1])
        {         
            backward[i] = 1;         
        }         
        else         
            backward[i] = backward[i + 1] + 1;         
    }         
            
    // To store the maximum length         
    int ans = 0;         
        
    // Find maximum length         
    for(int i = 0; i < N - 1; i++)
    {         
        if (A[i] != A[i + 1])         
            ans = Math.max(ans,         
                           Math.min(forward[i],         
                                    backward[i + 1]) * 2);         
    }         
     
    // Print the result         
    System.out.println(ans);         
}         
            
// Driver Code         
public static void main(String[] args)
{         
     
    // Given array         
    int arr[] = { 1, 2, 3, 4, 4,         
                  4, 6, 6, 6, 9 };         
            
    // Size of the array         
    int N = arr.length;         
            
    // Function call         
    maxLengthSubArray(arr, N);         
}         
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
 
# Function that finds the maximum
# length of the sub-array that
# contains equal element on both
# halves of sub-array
def maxLengthSubArray(A, N):
 
    # To store continuous occurence
    # of the element
    forward = [0] * N
    backward = [0] * N
 
    # To store continuous
    # forward occurence
    for i in range(N):
            if i == 0 or A[i] != A[i - 1]:
                forward[i] = 1
            else:
                forward[i] = forward[i - 1] + 1
 
    # To store continuous
    # backward occurence
    for i in range(N - 1, -1, -1):
        if i == N - 1 or A[i] != A[i + 1]:
            backward[i] = 1
        else:
            backward[i] = backward[i + 1] + 1
             
    # To store the maximum length
    ans = 0
 
    # Find maximum length
    for i in range(N - 1):
        if (A[i] != A[i + 1]):
            ans = max(ans,
                    min(forward[i],
                        backward[i + 1]) * 2);
 
    # Print the result
    print(ans)
 
# Driver Code
 
# Given array
arr = [ 1, 2, 3, 4, 4, 4, 6, 6, 6, 9 ]
 
# Size of the array
N = len(arr)
 
# Function call
maxLengthSubArray(arr, N)
 
# This code is contributed by yatinagg


C#
// C# program for the above approach         
using System;
class GFG{         
            
// Function that finds the maximum         
// length of the sub-array that         
// contains equal element on both         
// halves of sub-array         
static void maxLengthSubArray(int []A, int N)         
{
     
    // To store continuous occurence         
    // of the element         
    int []forward = new int[N];         
    int []backward = new int[N];         
     
    // To store continuous         
    // forkward occurence         
    for(int i = 0; i < N; i++)
    {
        if (i == 0 || A[i] != A[i - 1])
        {         
            forward[i] = 1;         
        }         
        else         
            forward[i] = forward[i - 1] + 1;         
    }         
     
    // To store continuous         
    // backward occurence         
    for(int i = N - 1; i >= 0; i--)
    {
        if (i == N - 1 || A[i] != A[i + 1])
        {         
            backward[i] = 1;         
        }         
        else         
            backward[i] = backward[i + 1] + 1;         
    }         
            
    // To store the maximum length         
    int ans = 0;         
        
    // Find maximum length         
    for(int i = 0; i < N - 1; i++)
    {         
        if (A[i] != A[i + 1])         
            ans = Math.Max(ans,         
                           Math.Min(forward[i],         
                                    backward[i + 1]) * 2);         
    }         
     
    // Print the result         
    Console.WriteLine(ans);         
}         
            
// Driver Code         
public static void Main(String[] args)
{         
     
    // Given array         
    int []arr = { 1, 2, 3, 4, 4,         
                  4, 6, 6, 6, 9 };         
            
    // Size of the array         
    int N = arr.Length;         
            
    // Function call         
    maxLengthSubArray(arr, N);         
}         
}
 
// This code is contributed by Princi Singh


输出:
6


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