Problem :-
Download the programme
- the largest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique.
Example :-
Input : arr[] = {3, 10, 2, 1, 20} Output : Length of LIS = 3
Algo : -
Input : arr[] Output : Length of LIS
for(i=1 to n) LIS[i]=1 for(i=2 to n) for(j=i-1 to 1) if(LIS[i]<LIS[j]+1) && (A[j]<A[i]) LIS[i]=LIS[j]+1Programme :-
Download the programme
#include<stdio.h> main() { int n,i,j; printf("ENTER THE SIZE OF ARRAY:"); scanf("%d",&n); int a[n],lis[n]; printf("ENTER ARRAY ELEMENTS:"); for(i=0;i<n;i++) { scanf("%d",&a[i]); lis[i]=1; } int max=0,maxInd; for(i=1;i<n;i++) { for(j=0;j<i;j++) { if(a[i]>a[j] && lis[i]<lis[j]+1) { lis[i]=lis[j]+1; } } if(lis[i]>max) { max=lis[i]; maxInd=i; } } printf("| LIS | = %d",max); /*for(j=0;j<n;j++) { printf("LIS[%d]: %d\n",j,lis[j]); }*/ }
Comments
Post a Comment