From eab6c5a85fb13d5d96add16cf395d231b76e8b1e Mon Sep 17 00:00:00 2001 From: Manas173 Date: Tue, 22 Oct 2019 16:00:17 +0530 Subject: [PATCH] Added Longest Increasing Subsequence --- .../longest-Increasing-Subsequence.c | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Data Structure & Algorithm/longest-Increasing-Subsequence.c diff --git a/Data Structure & Algorithm/longest-Increasing-Subsequence.c b/Data Structure & Algorithm/longest-Increasing-Subsequence.c new file mode 100644 index 0000000..a533f45 --- /dev/null +++ b/Data Structure & Algorithm/longest-Increasing-Subsequence.c @@ -0,0 +1,84 @@ +/* +This code has been written by Manas Ranjan Swain (https://github.com/Manas173/) + +Description: Given an array of N elements print the size of the longest icreasing subsequence present in the array. + +Input: +The first line contains an integer T, depicting total number of test cases. Then following T lines contains an integer N depicting the size of array and next line followed by the value of array. + +Output: +For each testcase, in a new line, print the Max length of the subsequence in a separate line. + + +Sample testcase: + +INPUT: +2 +8 +10 9 2 5 3 7 101 18 +6 +5 8 3 7 9 1 + +OUTPUT: +4 +3 + +EXPLANATION: +For the first testcase, 2,3,7,101 +For the second testcase, 5,7,9 and 3,7,9 are the possibilities +*/ + +#include + +int lis(int *arr,int *mem,int pos,int size) +{ + int max=1; + if(mem[pos]!=-1) + return mem[pos]; + + for(int i=pos+1;imax) + max=temp; + } + } + mem[pos]=max; + return mem[pos]; +} + +int main() +{ + int t; + scanf("%d",&t); + while(t--) + { + int size; + scanf("%d",&size); + int arr[size]; + int mem[size]; + + for(int i=0;i0) + { + mem[size-1]=1; + int ans=0; + for(int i=size-1;i>=0;i--) + { + mem[i]=lis(arr,mem,i,size); + if(ans