Maximum length of the sub-array whose first and last elements are same
Given a character array arr[] containing only lowercase English alphabets, the task is to print the maximum length of the subarray such that the first and the last element of the sub-array are same.
Examples:
Input: arr[] = {‘g’, ‘e’, ‘e’, ‘k’, ‘s’}
Output: 2
{‘e’, ‘e’} is the maximum length sub-array satisfying the given condition.
Input: arr[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘a’}
Output: 5
{‘a’, ‘b’, ‘c’, ‘d’, ‘a’} is the required sub-array
Approach: For every element of the array ch, store it’s first and last occurrence. Then the maximum length of the sub-array that starts and ends with the same element ch will be lastOccurrence(ch) – firstOccurrence(ch) + 1. The maximum of this value among all the elements is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Element
{
public :
int firstOcc, lastOcc;
Element();
void updateOccurence( int );
};
Element::Element()
{
firstOcc = lastOcc = -1;
}
void Element::updateOccurence( int index)
{
if (firstOcc == -1)
firstOcc = index;
lastOcc = index;
}
int maxLenSubArr(string arr, int n)
{
Element elements[26];
for ( int i = 0; i < n; i++)
{
int ch = arr[i] - 'a' ;
elements[ch].updateOccurence(i);
}
int maxLen = 0;
for ( int i = 0; i < 26; i++)
{
int len = elements[i].lastOcc -
elements[i].firstOcc + 1;
maxLen = max(maxLen, len);
}
return maxLen;
}
int main()
{
string arr = "geeks" ;
int n = arr.length();
cout << maxLenSubArr(arr, n) << endl;
return 0;
}
|
Java
class Element {
int firstOcc, lastOcc;
public Element()
{
firstOcc = lastOcc = - 1 ;
}
public void updateOccurrence( int index)
{
if (firstOcc == - 1 )
firstOcc = index;
lastOcc = index;
}
}
class GFG {
public static int maxLenSubArr( char arr[], int n)
{
Element elements[] = new Element[ 26 ];
for ( int i = 0 ; i < n; i++) {
int ch = arr[i] - 'a' ;
if (elements[ch] == null )
elements[ch] = new Element();
elements[ch].updateOccurrence(i);
}
int maxLen = 0 ;
for ( int i = 0 ; i < 26 ; i++) {
if (elements[i] != null ) {
int len = elements[i].lastOcc - elements[i].firstOcc + 1 ;
maxLen = Math.max(maxLen, len);
}
}
return maxLen;
}
public static void main(String[] args)
{
char arr[] = { 'g' , 'e' , 'e' , 'k' , 's' };
int n = arr.length;
System.out.print(maxLenSubArr(arr, n));
}
}
|
Python3
class Element:
def __init__( self ):
self .firstOcc = - 1
self .lastOcc = - 1
def updateOccurrence( self , index):
if self .firstOcc = = - 1 :
self .firstOcc = index
self .lastOcc = index
def maxLenSubArr(arr, n):
elements = [ None ] * 26
for i in range ( 0 , n):
ch = ord (arr[i]) - ord ( 'a' )
if elements[ch] = = None :
elements[ch] = Element()
elements[ch].updateOccurrence(i)
maxLen = 0
for i in range ( 0 , 26 ):
if elements[i] ! = None :
length = (elements[i].lastOcc -
elements[i].firstOcc + 1 )
maxLen = max (maxLen, length)
return maxLen
if __name__ = = "__main__" :
arr = [ 'g' , 'e' , 'e' , 'k' , 's' ]
n = len (arr)
print (maxLenSubArr(arr, n))
|
C#
using System;
public class Element
{
public int firstOcc, lastOcc;
public Element()
{
firstOcc = lastOcc = -1;
}
public void updateOccurrence( int index)
{
if (firstOcc == -1)
firstOcc = index;
lastOcc = index;
}
}
class GFG
{
public static int maxLenSubArr( char []arr, int n)
{
Element []elements = new Element[26];
for ( int i = 0; i < n; i++)
{
int ch = arr[i] - 'a' ;
if (elements[ch] == null )
elements[ch] = new Element();
elements[ch].updateOccurrence(i);
}
int maxLen = 0;
for ( int i = 0; i < 26; i++)
{
if (elements[i] != null )
{
int len = elements[i].lastOcc - elements[i].firstOcc + 1;
maxLen = Math.Max(maxLen, len);
}
}
return maxLen;
}
public static void Main()
{
char []arr = { 'g' , 'e' , 'e' , 'k' , 's' };
int n = arr.Length;
Console.WriteLine(maxLenSubArr(arr, n));
}
}
|
Javascript
<script>
class Element
{
constructor()
{
this .firstOcc = -1;
this .lastOcc = -1;
}
updateOccurrence(index){
if ( this .firstOcc == -1)
this .firstOcc = index;
this .lastOcc = index;
}
}
function maxLenSubArr(arr, n)
{
let elements = new Array(26);
for (let i=0;i<26;i++)
{
elements[i] = new Element();
}
for (let i=0;i<n;i++)
{
let ch = arr[i].charCodeAt(0) - 'a' .charCodeAt(0);
elements[ch].updateOccurrence(i);
}
let maxLen = 0;
for (let i=0;i<26;i++)
{
let len = elements[i].lastOcc -
elements[i].firstOcc + 1;
maxLen = Math.max(maxLen, len);
}
return maxLen;
}
let arr = "geeks" ;
let n = arr.length;
console.log(maxLenSubArr(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Last Updated :
06 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...