my code is only working for testcase 1
Not able to solve
hello @sindhu_21
a) ur array is small , check maximum value of n , and declare ur array with size greater than that.
b) ur solution time complexity is O(tnn) which will not work for this problem.
another approach
Let us consider the array {12, 4, 78, 90, 45, 23} to understand the solution.
-
Construct an auxiliary array inc[] from left to right such that inc[i] contains length of the nondecreaing subarray ending at arr[i].
For A[] = {12, 4, 78, 90, 45, 23}, inc[] is {1, 1, 2, 3, 1, 1} -
Construct another array dec[] from right to left such that dec[i] contains length of nonincreasing subarray starting at arr[i].
For A[] = {12, 4, 78, 90, 45, 23}, dec[] is {2, 1, 1, 3, 2, 1}. -
Once we have the inc[] and dec[] arrays, all we need to do is find the maximum value of (inc[i] + dec[i] ā 1).
For {12, 4, 78, 90, 45, 23}, the max value of (inc[i] + dec[i] ā 1) is 5 for i = 3.int bitonic(int arr[], int n)
{
int inc[n]; // Length of increasing subarray ending at all indexes
int dec[n]; // Length of decreasing subarray starting at all indexes
int i, max;// length of increasing sequence ending at first index is 1
inc[0] = 1;// length of increasing sequence starting at first index is 1
dec[n-1] = 1;// Step 1) Construct increasing sequence array
for (i = 1; i < n; i++)
inc[i] = (arr[i] >= arr[i-1])? inc[i-1] + 1: 1;// Step 2) Construct decreasing sequence array
for (i = n-2; i >= 0; iā)
dec[i] = (arr[i] >= arr[i+1])? dec[i+1] + 1: 1;// Step 3) Find the length of maximum length bitonic sequence
max = inc[0] + dec[0] - 1;
for (i = 1; i < n; i++)
if (inc[i] + dec[i] - 1 > max)
max = inc[i] + dec[i] - 1;return max;
}