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: 4
Explanation:
{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: 6
Explanation:
{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(N3)
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 min(forward[i], backward[i+1])*2, for all the index where arr[i]!=arr[i+1].
- Print the value obtained in the above step.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxLengthSubArray( int A[], int N)
{
int forward[N], backward[N];
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;
}
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;
}
int ans = 0;
for ( int i = 0; i < N - 1; i++) {
if (A[i] != A[i + 1])
ans = max(ans,
min(forward[i],
backward[i + 1])
* 2);
}
cout << ans;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 4,
4, 6, 6, 6, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
maxLengthSubArray(arr, N);
return 0;
}
|
Java
class GFG{
static void maxLengthSubArray( int A[], int N)
{
int forward[] = new int [N];
int backward[] = new int [N];
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 ;
}
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 ;
}
int ans = 0 ;
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 );
}
System.out.println(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 4 ,
4 , 6 , 6 , 6 , 9 };
int N = arr.length;
maxLengthSubArray(arr, N);
}
}
|
Python3
def maxLengthSubArray(A, N):
forward = [ 0 ] * N
backward = [ 0 ] * N
for i in range (N):
if i = = 0 or A[i] ! = A[i - 1 ]:
forward[i] = 1
else :
forward[i] = forward[i - 1 ] + 1
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
ans = 0
for i in range (N - 1 ):
if (A[i] ! = A[i + 1 ]):
ans = max (ans,
min (forward[i],
backward[i + 1 ]) * 2 );
print (ans)
arr = [ 1 , 2 , 3 , 4 , 4 , 4 , 6 , 6 , 6 , 9 ]
N = len (arr)
maxLengthSubArray(arr, N)
|
C#
using System;
class GFG{
static void maxLengthSubArray( int []A, int N)
{
int []forward = new int [N];
int []backward = new int [N];
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;
}
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;
}
int ans = 0;
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);
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3, 4, 4,
4, 6, 6, 6, 9 };
int N = arr.Length;
maxLengthSubArray(arr, N);
}
}
|
Javascript
<script>
function maxLengthSubArray(A, N)
{
let forward = Array.from({length: N}, (_, i) => 0);
let backward = Array.from({length: N}, (_, i) => 0);
for (let i = 0; i < N; i++)
{
if (i == 0 || A[i] != A[i - 1])
{
forward[i] = 1;
}
else
forward[i] = forward[i - 1] + 1;
}
for (let 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;
}
let ans = 0;
for (let i = 0; i < N - 1; i++)
{
if (A[i] != A[i + 1])
ans = Math.max(ans,
Math.min(forward[i],
backward[i + 1]) * 2);
}
document.write(ans);
}
let arr = [ 1, 2, 3, 4, 4,
4, 6, 6, 6, 9 ];
let N = arr.length;
maxLengthSubArray(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)