Maximum Length Bitonic Subarray | GeeksforGeeks
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.
-- end at i, start at i.
We can save space, 2n to n, using the trick from http://massivealgorithms.blogspot.com/2014/07/dynamic-programming-set-15-longest.html
O(1) space
http://www.techiedelight.com/find-longest-bitonic-subarray-array/
void findBitonicSubarray(int A[], int n)
{
int end_index = 0, max_len = 0;
int i = 0;
while (i + 1 < n)
{
// check for Longest Bitonic Subarray starting at A[i]
// reset length to 1
int len = 1;
// run till sequence is increasing
while (i + 1 < n && A[i] < A[i + 1])
i++, len++;
// run till sequence is decreasing
while (i + 1 < n && A[i] > A[i + 1])
i++, len++;
// update Longest Bitonic Subarray if required
if (len > max_len)
{
max_len = len;
end_index = i;
}
}
// print longest bitonic sub-array
printf("The length of longest bitonic sub-array is %d\n", max_len);
printf("The longest bitonic sub-array is [%d, %d]",
end_index - max_len + 1, end_index);
}
http://www.geeksforgeeks.org/maximum-length-bitonic-subarray-set-2-time-o1-space/
Dynamic Programming | Set 15 (Longest Bitonic Subsequence)
http://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/
We need to construct two arrays lis[] and lds[] using Dynamic Programming solution of LIS problem. lis[i] stores the length of the Longest Increasing subsequence ending with arr[i]. lds[i] stores the length of the longest Decreasing subsequence starting from arr[i]. Finally, we need to return the max value of lis[i] + lds[i] – 1 where i is from 0 to n-1.
Time Complexity: O(n^2)
Auxiliary Space: O(n)
http://www.techiedelight.com/find-maximum-difference-between-two-elements-array/
Input: { 2, 7, 9, 5, 1, 3, 5 }
int diff(int arr[], int n)
{
int diff = INT_MIN;
int max_so_far = arr[n-1];
// traverse the array from right and keep track the maximum element
for (int i = n - 2; i >= 0; i--)
{
// update max if current element is greater than the maximum element
if (arr[i] > max_so_far)
max_so_far = arr[i];
// if the current element is less than the maximum element,
// then update the difference if required
else
diff = max (diff, max_so_far - arr[i]);
}
// return difference
return diff;
}
int diff(int arr[], int n)
{
int diff = INT_MIN;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
diff = max(diff, arr[j] - arr[i]);
return diff;
}
Read full article from Maximum Length Bitonic Subarray | GeeksforGeeks
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.
1) Construct an auxiliary array inc[] from left to right such that inc[i] contains length of the nondecreaing subarray ending at arr[i].
2) Construct another array dec[] from right to left such that dec[i] contains length of nonincreasing subarray starting at arr[i].
2) Construct another array dec[] from right to left such that dec[i] contains length of nonincreasing subarray starting at arr[i].
3) Once we have the inc[] and dec[] arrays, all we need to do is find the maximum value of (inc[i] + dec[i] – 1)
Similar Algorithmhttp://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/-- end at i, start at i.
We can save space, 2n to n, using the trick from http://massivealgorithms.blogspot.com/2014/07/dynamic-programming-set-15-longest.html
static
int
bitonic(
int
arr[],
int
n)
{
int
[] inc =
new
int
[n];
// Length of increasing subarray ending
// at all indexes
int
[] dec =
new
int
[n];
// Length of decreasing subarray starting
// at all indexes
int
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
(
int
i =
1
; i < n; i++)
inc[i] = (arr[i] >= arr[i-
1
])? inc[i-
1
] +
1
:
1
;
// Step 2) Construct decreasing sequence array
for
(
int
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
(
int
i =
1
; i < n; i++)
if
(inc[i] + dec[i] -
1
> max)
max = inc[i] + dec[i] -
1
;
return
max;
}
http://www.techiedelight.com/find-longest-bitonic-subarray-array/
void findBitonicSubarray(int A[], int n)
{
int end_index = 0, max_len = 0;
int i = 0;
while (i + 1 < n)
{
// check for Longest Bitonic Subarray starting at A[i]
// reset length to 1
int len = 1;
// run till sequence is increasing
while (i + 1 < n && A[i] < A[i + 1])
i++, len++;
// run till sequence is decreasing
while (i + 1 < n && A[i] > A[i + 1])
i++, len++;
// update Longest Bitonic Subarray if required
if (len > max_len)
{
max_len = len;
end_index = i;
}
}
// print longest bitonic sub-array
printf("The length of longest bitonic sub-array is %d\n", max_len);
printf("The longest bitonic sub-array is [%d, %d]",
end_index - max_len + 1, end_index);
}
http://www.geeksforgeeks.org/maximum-length-bitonic-subarray-set-2-time-o1-space/
static
int
maxLenBitonic(
int
[] A,
int
n)
{
// if A is empty
if
(n ==
0
)
return
0
;
// initializing max_len
int
maxLen=
1
;
int
start=
0
;
int
nextStart=
0
;
int
j =
0
;
while
(j < n-
1
)
{
// look for end of ascent
while
(j<n-
1
&& A[j]<=A[j+
1
])
j++;
// look for end of descent
while
(j<n-
1
&& A[j]>=A[j+
1
]){
// adjusting nextStart;
// this will be necessarily executed at least once,
// when we detect the start of the descent
if
(j<n-
1
&& A[j]>A[j+
1
])
nextStart=j+
1
;
j++;
}
// updating maxLen, if required
maxLen = Math.max(maxLen,j-(start-
1
));
start=nextStart;
}
return
maxLen;
}
http://www.geeksforgeeks.org/dynamic-programming-set-15-longest-bitonic-subsequence/
We need to construct two arrays lis[] and lds[] using Dynamic Programming solution of LIS problem. lis[i] stores the length of the Longest Increasing subsequence ending with arr[i]. lds[i] stores the length of the longest Decreasing subsequence starting from arr[i]. Finally, we need to return the max value of lis[i] + lds[i] – 1 where i is from 0 to n-1.
Time Complexity: O(n^2)
Auxiliary Space: O(n)
/* lbs() returns the length of the Longest Bitonic Subsequence in
arr[] of size n. The function mainly creates two temporary arrays
lis[] and lds[] and returns the maximum lis[i] + lds[i] - 1.
lis[i] ==> Longest Increasing subsequence ending with arr[i]
lds[i] ==> Longest decreasing subsequence starting with arr[i]
*/
int
lbs(
int
arr[],
int
n )
{
int
i, j;
/* Allocate memory for LIS[] and initialize LIS values as 1 for
all indexes */
int
*lis =
new
int
[n];
for
( i = 0; i < n; i++ )
lis[i] = 1;
/* Compute LIS values from left to right */
for
( i = 1; i < n; i++ )
for
( j = 0; j < i; j++ )
if
( arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
/* Allocate memory for lds and initialize LDS values for
all indexes */
int
*lds =
new
int
[n];
for
( i = 0; i < n; i++ )
lds[i] = 1;
/* Compute LDS values from right to left */
for
( i = n-2; i >= 0; i-- )
for
( j = n-1; j > i; j-- )
if
( arr[i] > arr[j] && lds[i] < lds[j] + 1)
lds[i] = lds[j] + 1;
/* Return the maximum value of lis[i] + lds[i] - 1*/
int
max = lis[0] + lds[0] - 1;
for
(i = 1; i < n; i++)
if
(lis[i] + lds[i] - 1 > max)
max = lis[i] + lds[i] - 1;
return
max;
}
Given an array of integers, find the maximum difference between two elements in the array such that smaller element appears before the larger element.
For example,
Input: { 2, 7, 9, 5, 1, 3, 5 }
Output: The maximum difference is 7
The pair is (2, 9)
We can solve this problem in linear time. The idea is to traverse the array from the right and keep track of maximum difference found so far. If the current element is less than the maximum element found so far and their difference is more than maximum difference found so far, then we update the maximum difference with current difference.The pair is (2, 9)
int diff(int arr[], int n)
{
int diff = INT_MIN;
int max_so_far = arr[n-1];
// traverse the array from right and keep track the maximum element
for (int i = n - 2; i >= 0; i--)
{
// update max if current element is greater than the maximum element
if (arr[i] > max_so_far)
max_so_far = arr[i];
// if the current element is less than the maximum element,
// then update the difference if required
else
diff = max (diff, max_so_far - arr[i]);
}
// return difference
return diff;
}
int diff(int arr[], int n)
{
int diff = INT_MIN;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
diff = max(diff, arr[j] - arr[i]);
return diff;
}
Read full article from Maximum Length Bitonic Subarray | GeeksforGeeks