(1)
/*
* input: an array a[], the length of the array n
* output: the length of the consecutive segment
*/
int maxSub(int a[], int n)
{
if (n == 0)
return 0;
int length = 1;
int longestLength = 1;
for (int i = 1; i<n; i++)
{
if (a[i] == a[i-1])
{
length++;
longestLength = max(longestLength, length);
}
else
length = 1;
}
return longestLength;
}
(2)
/*
* input: an sorted array a[] (increasing order), the length of the array n
* output: the length of the consecutive segment
*/
int maxSub(int a[], int n)
{
if (n == 0)
return 0;
int length = 1;
for (int i = 1; i<n; i++)
{
if (a[i] == a[i-length])
length++;
}
return length;
}