Given an array **arr[]** of **N** integers, the task is to find the maximum length of sub-array consisting of the same type of element on both halves of the sub-array. Also, the elements on both halves differ from each other.

**Examples:**

Input:arr[] = {2, 3, 4, 4, 5, 5, 6, 7, 8, 10}Output:4Explanation:{2, 3}, {3, 4}, {4, 4, 5, 5}, {5, 6}, etc, are the valid sub-arrays where both halves have only one type of element.{4, 4, 5, 5} is the sub-array having maximum length.Hence, the output is 4.

Input:arr[] = {1, 7, 7, 10, 10, 7, 7, 7, 8, 8, 8, 9}Output:6Explanation:{1, 7}, {7, 7, 10, 10}, {7, 7, 7, 8, 8, 8}, {8, 9}, etc, are the valid sub-arrays where both halves have only one type of element.{7, 7, 7, 8, 8, 8} is the sub-array having maximum length.Hence, the output is 6.

**Naive Approach:** The naive idea is to generate all possible subarray and check any subarray with maximum length can be divided into two halves such that all the elements in both the halves are the same.

**Time Complexity:** O(N^{3})**Auxiliary Space:** O(1)

**Efficient Approach: **To solve this problem the idea is to use the concept of Prefix Sum. Follow the steps below to solve the problem:

- Traverse the array from the start in the forward direction and store the continuous occurrence of an integer for each index in an array
**forward[].** - Similarly, traverse the array from the end in the reverse direction and store the continuous occurrence of an integer for each index in an array
**backward[]**. - Store the maximum of
for all the index where**min(forward[i], backward[i+1])*2,****arr[i]!=arr[i+1]**. - Print the value obtained in the above step.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output:**

6

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum length of subarray such that all elements are equal in the subarray
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Maximum length of subarray such that sum of the subarray is even
- Maximum sum subarray of even length
- Length of maximum product subarray
- Length of longest subarray of length at least 2 with maximum GCD
- Min and max length subarray having adjacent element difference atmost K
- Find subarray of Length K with Maximum Peak
- Find maximum average subarray of k length
- Length of longest Fibonacci subarray formed by removing only one element
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Maximum number of consecutive 1s after flipping all 0s in a K length subarray
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
- Maximum sum subarray removing at most one element
- Maximum Unique Element in every subarray of size K
- Subarray with largest sum after excluding its maximum element
- Maximize the maximum subarray sum after removing atmost one element
- Maximum count of sub-strings of length K consisting of same characters
- Length of the smallest sub-string consisting of maximum distinct characters

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.