jain12

longest increasing subsequence by recursion

May 1st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4.  
  5.  
  6. int _LIS(int arr[],int n,int *max_ref){
  7.   if(n==1)
  8.     return 1;
  9.   int res=0,max_ending_here=0;
  10.   for(int i=1;i<n;i++){
  11.     res=_LIS(arr,i,max_ref);
  12.     if(arr[i-1]<arr[n-1] && res+1 >max_ending_here)
  13.         max_ending_here=res+1;
  14.     }
  15.   if(*max_ref<max_ending_here)
  16.      *max_ref=max_ending_here;
  17.   return max_ending_here;
  18.   }
  19.  
  20. int LIS(int arr[],int n){
  21.   if(n==0)
  22.     return n;
  23.   int max=1;
  24.   _LIS(arr,n,&max);
  25.   return max;
  26.   }
  27.  
  28. int main(){
  29.   int n=8;
  30.   int arr[] = {10,22,9, 33, 21, 50, 41, 60 };
  31.   cout<<LIS(arr,n);
  32.   return 0;
  33.   }
Add Comment
Please, Sign In to add comment