Given an array A[0 … n-1] containing n positive integers, a subarray A[i … j] is bitonic if there is a k with i <= k <= j such that A[i] <= A[i + 1] ... <= A[k] >= A[k + 1] >= .. A[j – 1] > = A[j]. Write a function that takes an array as argument and returns the length of the maximum length bitonic subarray.
Solution:
Solution:
int maxBitonicSubArrayLen(int *arr, int len)
{
if(arr == NULL || len == 0)
return 0;
bool isDecreasing = false;
int max = 1, count = 1;
for(int i = 0; i < len - 1; ++i)
{
if(arr[i] <= arr[i+1])
{
if(isDecreasing)
{
count = 1;
isDecreasing = false;
}
++count;
}
else
{
if(!isDecreasing)
isDecreasing = true;
++count;
}
if(max < count)
max = count;
}
return max;
}
No comments:
Post a Comment