Longest Bitonic subsequence | Practice | GeeksforGeeks

Given an array of positive integers. Find the maximum length of Bitonic subsequence. 
A subsequence of array is called Bitonic if it is first increasing, then decreasing.
 
Example 1:
Input: nums = [1, 2, 5, 3, 2]
Output: 5
Explanation: The sequence {1, 2, 5} is
increasing and the sequence {3, 2} is 
decreasing so merging both we will get 
length 5.

Example 2:
Input: nums = [1, 11, 2, 10, 4, 5, 2, 1]
Output: 6
Explanation: The bitonic sequence 
{1, 2, 10, 4, 2, 1} has length 6.

 
Your Task:
You don't need to read or print anything. Your task is to complete the function LongestBitonicSequence() which takes the array nums[] as input parameter and returns the maximum length of bitonic subsequence.
 
Expected Time Complexity: O(n2)
Expected Space Complexity: O(n)
 
Constraints:
1 ≤ length of array ≤ 200
1 ≤ arr[i] ≤ 1000000
0
aayushrawat2 weeks ago
C++ Solution 
The idea here is to calculate the Longest Increasing Subsequence and Longest Decreasing Subsequence separately. After that, we will iterate through them and will get the maximum length by adding lis[i] and lds[i];
 
	int LongestBitonicSequence(vector<int>nums)
	{
	    // code here
  int len = nums.size();
  vector<int> lis(len,1);
  vector<int> lds(len,1);
  for(int i=1;i<len;i++){
      for(int j=0;j<i;j++){
           if(nums[j]<nums[i] && lis[i]<=lis[j]){
               lis[i] = lis[j]+1;
           }
      }
  }
  for(int i = len-2;i>=0;i--){
      for(int j = len-1;j>i;j--){
          if(nums[j]<nums[i] && lds[i]<=lds[j] ){
              lds[i] = lds[j]+1;
          }
      }
  }
  //We are subtracting -1 here because the last number in LIS and LDS will be same, and we
  //do not want to count it twice. 5 in this case.
  int maxV = 0;
  for(int i=0;i<len;i++){
      maxV = max(maxV , lis[i]+lds[i]-1);
  }
  return maxV;

	}
0
madhukartemba
This comment was deleted.
+2
madhukartemba2 weeks ago
JAVA SOLUTION:
class Solution
{
    public int LongestBitonicSequence(int[] arr)
    {
        int n = arr.length;
        int dp_left[] = new int[n];
        int dp_right[] = new int[n];
        
        int ans = 1;
        
        for(int i=0; i<n; i++)
        {
            int max_len = 0;
            for(int j=0; j<i; j++)
            {
                if(arr[j]<arr[i])
                {
                    max_len = Math.max(dp_left[j], max_len);
                }
            }
            
            dp_left[i] = max_len + 1;
            ans = Math.max(ans, dp_left[i]);
        }
        
        
        for(int i=n-1; i>=0; i--)
        {
            int max_len = 0;
            
            for(int j=i+1; j<n; j++)
            {
                if(arr[j]<arr[i])
                {
                    max_len = Math.max(dp_right[j], max_len);
                }
            }
            
            dp_right[i] = max_len + 1;
            ans = Math.max(ans, dp_right[i]);
            
        }
        
        
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if(arr[j]<arr[i])
                {
                    ans = Math.max(dp_left[i]+dp_right[j], ans);
                }
            }
        }
        
        
        return ans;
        
    }
}
0
vishalkumar13july3 weeks ago
int LongestBitonicSequence(vector<int>nums){    // code here    int n=nums.size();   //     int i,j;    int *lis=new int[n];    for( i=0;i<n;i++){        lis[i]=1;    }    for(i=1;i<n;i++){        for(j=0;j<i;j++){            if(nums[j]<nums[i] && lis[i]<lis[j]+1)                lis[i]=lis[j]+1;        }    }    int *lds=new int[n];    for(i=0;i<n;i++){        lds[i]=1;    }    for(i=n-2;i>=0;i--){        for(j=n-1;j>i;j--){            if(nums[j]<nums[i] && lds[i]<lds[j]+1)                lds[i]=lds[j]+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;    }    delete [] lis;    delete [] lds;    return max;}
0
himanshukug19cs3 months ago
java solution
 // Code here       int n=nums.length;       int ans=Integer.MIN_VALUE;       int[] ic = new int[n];       int[] dc = new int[n];       ic[0]=1;       dc[n-1]=1;       for(int i=1;i<n;i++){           ic[i]=1;           for(int j=0;j<i;j++){               if(nums[i]>nums[j])                   ic[i]=Math.max(ic[i],1+ic[j]);       }       }       for(int i=n-2;i>=0;i--){           dc[i]=1;           for(int j=n-1;j>i;j--){               if(nums[j]<nums[i])                  dc[i]=Math.max(dc[i],dc[j]+1);           }       }       for(int i=0;i<n;i++){           ans=Math.max(ans,ic[i]+dc[i]-1);       }                  return ans;   }
0
baibhavrajputt3 months ago
Java DP solution
class Solution
{
    public int LongestBitonicSequence(int[] nums)
    {
        // Code here
        int n = nums.length;
        
        int lis[] = new int[n];
        int lds[] = new int[n];
        
        lis[0] = 1;
        lds[n-1] = 1;
        
        for(int i=1 ; i<n ; i++){
            lis[i] = 1;
            for(int j=0 ; j<i ; j++){
                
                if(nums[i] > nums[j])
                    lis[i] = Math.max(lis[i] , lis[j] + 1);
            }
        }
        
        
        for(int i=n-2 ; i>=0 ; i--){
            lds[i] = 1;
            for(int j=n-1 ; j>i ; j--){
                
                if(nums[i] > nums[j])
                    lds[i] = Math.max(lds[i] , lds[j] + 1);
            }
        }
    
        int lbs =0;
        for(int i=0 ; i<n ; i++){
            lbs = Math.max(lbs , lis[i]+lds[i]-1);
        }
        return lbs;
    }
}
0
aaishuagarwal3 months ago
 int LongestBitonicSequence(vector<int>nums){    // code here    int n=nums.size();    int lis[n],lds[n];    lis[0]=1;    for(int i=1;i<n;i++)    {       lis[i]=1;       for(int j=0;j<i;j++)       {           if(nums[j]<nums[i])           {               lis[i]=max(lis[i],lis[j]+1);           }       }    }    lds[n-1]=1;    for(int i=n-2;i>=0;i--)    {        lds[i]=1;        for(int j=n-1;j>i;j--)        {            if(nums[j]<nums[i])            {                lds[i]=max(lds[i],lds[j]+1);            }        }    }    for(int i=0;i<n;i++)    lis[i]+=lds[i];    int m=INT_MIN;    for(int i=0;i<n;i++)    {        m=max(m,lis[i]);    }    return m-1;}
0
amishasahu3283 months ago
class Solution{
	public:
	
	int LongestBitonicSequence(vector<int>nums)
	{
	    // code here
	    int n = nums.size();
	    int lis[n], lds[n];
	    
	    lis[0] = 1;
	    lds[n-1] = 1;
	    
	    for(int i = 1; i < n; i++)
	    {
	        lis[i] = 1;
	        for(int j = 0; j < i; j++)
	        {
	            if(nums[j] < nums[i])
	                lis[i] = max(lis[i], lis[j]+1);
	        }
	    }
	     for(int i = n-2; i >= 0; i--)
	     {
	         lds[i] = 1;
	        for(int j = n-1; j > i; j--)
	        {
	            if(nums[j] < nums[i])
	                lds[i] = max(lds[i], lds[j]+1);
	        }
	    }
	    
	    int ans = 0;
	    for(int i = 0; i < n; i++)
	    {
	        ans = max(ans, lis[i]+lds[i]-1);
	    }
	    
	    return ans;
	}
};
0
taiphanvan24034 months ago
int Binotic_subsequence(int a[], int n){int* ListIncre = new int[n];int* ListDecre = new int[n];ListIncre[0] = 1;for (int i = 1; i < n; i++){ ListIncre[i] = 1; for (int j = 0; j < i; j++) {  if (a[j]<a[i] && ListIncre[j] + 1>ListIncre[i]) ListIncre[i] = ListIncre[j] + 1; }}ListDecre[n - 1] = 1;for (int i = n - 2; i >= 0; i--){ ListDecre[i] = 1; for (int j = n - 1; j > i; j--) {  if (a[j]<a[i] && ListDecre[j]+1>ListDecre[i]) ListDecre[i] = ListDecre[j] + 1; }}int Max = ListIncre[0] + ListDecre[0];for (int i = 1; i < n; i++) Max = max(Max, ListIncre[i] + ListDecre[i]);return Max - 1;}int main(){int a[] = { 12, 11, 40, 5, 3, 1 };int n = sizeof(a) / 4;cout<<Binotic_subsequence(a, n);}
0
sparshrastogi14 months ago
#Python code
def LongestBitonicSequence(self, nums): # Code here l = len(nums) f = [1]*l b = [1]*l for i in range(l-1):     for j in range(i+1,l):         if nums[i]<nums[j]:             f[j] = max(f[j], f[i]+1)         if nums[l-i-1]<nums[l-j-1]:             b[l-j-1] = max(b[l-i-1]+1, b[l-j-1]) res = 0 for i in range(l):     res = max(res, f[i]+b[i]-1) return res
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.