0% found this document useful (0 votes)
36 views1 page

Longest Increasing Subsequence

This C program finds the length of the longest increasing subsequence in an array. It takes an array and its size as input, allocates memory to store the lengths of increasing subsequences, iterates through the array to find the maximum subsequence length for each element, and returns the maximum length found. The time complexity is O(n2) and space complexity is O(n).

Uploaded by

anup semwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views1 page

Longest Increasing Subsequence

This C program finds the length of the longest increasing subsequence in an array. It takes an array and its size as input, allocates memory to store the lengths of increasing subsequences, iterates through the array to find the maximum subsequence length for each element, and returns the maximum length found. The time complexity is O(n2) and space complexity is O(n).

Uploaded by

anup semwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Maximum length of longest increasing sub sequence

#include <stdio.h>
#include <stdlib.h>

int LongestIncreasingSubsequence(int *arr, int size)


{
int *lis, i, j, maxLen = 0;
lis = (int *)malloc(sizeof(int) * size);

for(i = 0; i < size; i++)


lis[i] = 1;

for (i = 1; i < size; i++ )


for (j = 0; j < i; j++ )
if ( arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;

for (i = 0; i < size; i++ )


if (maxLen < lis[i])
maxLen = lis[i];
free(lis);
return maxLen;
}

int main()
{
int *arr, size;
printf("Enter size of the array\n");
scanf("%d", &size);

//allocate memory
arr = (int *)malloc(sizeof(int) * size);

printf("Enter elements in array\n");


for(int index = 0; index < size; index++)
scanf("%d", &arr[index]);

printf("Length of longest increasing sub sequence is = %d",


LongestIncreasingSubsequence(arr, size));

return 0;
}

Time complexity: O(n2)


Space complexity: O(n)

You might also like