{"id":26243,"date":"2017-10-26T20:41:28","date_gmt":"2017-10-26T15:11:28","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26243"},"modified":"2017-10-26T20:41:28","modified_gmt":"2017-10-26T15:11:28","slug":"java-programming-longest-bitonic-subsequence","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-longest-bitonic-subsequence\/","title":{"rendered":"Java Programming &#8211; Longest Bitonic Subsequence"},"content":{"rendered":"<p>Given an array arr[0 \u2026 n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence.<span id=\"more-19729\"><\/span><br \/>\nA sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty.<\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre>Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1};\r\nOutput: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1)\r\n\r\nInput arr[] = {12, 11, 40, 5, 3, 1}\r\nOutput: 5 (A Longest Bitonic Subsequence of length 5 is 12, 11, 5, 3, 1)\r\n\r\nInput arr[] = {80, 60, 30, 40, 20, 10}\r\nOutput: 5 (A Longest Bitonic Subsequence of length 5 is 80, 60, 30, 20, 10)<\/pre>\n<p><strong>Solution<\/strong><br \/>\nThis problem is a variation of standard Longest Increasing Subsequence (LIS) problem. Let the input array be arr[] of length n. We need to construct two arrays lis[] and lds[] using Dynamic Programming solution of LIS problem. lis[i] stores the length of the Longest Increasing subsequence ending with arr[i]. lds[i] stores the length of the longest Decreasing subsequence starting from arr[i]. Finally, we need to return the max value of lis[i] + lds[i] \u2013 1 where i is from 0 to n-1.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is Java\u00a0implementation of the above Dynamic Programming solution.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/* Dynamic Programming implementation in Java for longest bitonic<br\/>   subsequence problem *\/<br\/>import java.util.*;<br\/>import java.lang.*;<br\/>import java.io.*;<br\/> <br\/>class LBS<br\/>{<br\/>    \/* lbs() returns the length of the Longest Bitonic Subsequence in<br\/>    arr[] of size n. The function mainly creates two temporary arrays<br\/>    lis[] and lds[] and returns the maximum lis[i] + lds[i] - 1.<br\/> <br\/>    lis[i] ==&gt; Longest Increasing subsequence ending with arr[i]<br\/>    lds[i] ==&gt; Longest decreasing subsequence starting with arr[i]<br\/>    *\/<br\/>    static int lbs( int arr[], int n )<br\/>    {<br\/>        int i, j;<br\/> <br\/>        \/* Allocate memory for LIS[] and initialize LIS values as 1 for<br\/>            all indexes *\/<br\/>        int[] lis = new int[n];<br\/>        for (i = 0; i &lt; n; i++)<br\/>            lis[i] = 1;<br\/> <br\/>        \/* Compute LIS values from left to right *\/<br\/>        for (i = 1; i &lt; n; i++)<br\/>            for (j = 0; j &lt; i; j++)<br\/>                if (arr[i] &gt; arr[j] &amp;&amp; lis[i] &lt; lis[j] + 1)<br\/>                    lis[i] = lis[j] + 1;<br\/> <br\/>        \/* Allocate memory for lds and initialize LDS values for<br\/>            all indexes *\/<br\/>        int[] lds = new int [n];<br\/>        for (i = 0; i &lt; n; i++)<br\/>            lds[i] = 1;<br\/> <br\/>        \/* Compute LDS values from right to left *\/<br\/>        for (i = n-2; i &gt;= 0; i--)<br\/>            for (j = n-1; j &gt; i; j--)<br\/>                if (arr[i] &gt; arr[j] &amp;&amp; lds[i] &lt; lds[j] + 1)<br\/>                    lds[i] = lds[j] + 1;<br\/> <br\/> <br\/>        \/* Return the maximum value of lis[i] + lds[i] - 1*\/<br\/>        int max = lis[0] + lds[0] - 1;<br\/>        for (i = 1; i &lt; n; i++)<br\/>            if (lis[i] + lds[i] - 1 &gt; max)<br\/>                max = lis[i] + lds[i] - 1;<br\/> <br\/>        return max;<br\/>    }<br\/> <br\/>    public static void main (String[] args)<br\/>    {<br\/>        int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5,<br\/>                    13, 3, 11, 7, 15};<br\/>        int n = arr.length;<br\/>        System.out.println(&quot;Length of LBS is &quot;+ lbs( arr, n ));<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre> Length of LBS is 7\r\n<\/pre>\n<p>Time Complexity: O(n^2)<br \/>\nAuxiliary Space: O(n)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Java Programming &#8211; Longest Bitonic Subsequence &#8211; Dynamic Programming Array arr[0.n-1] containing positive integers, a subsequence of arr[] is called Bitonic<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,70145,2139],"tags":[78010,78005,78006,78008,78003,72847,72842,72845,70483,78007,72848,72840,78009,72846,72987,72985,72843,72854,72844,72850,78002,72839,78001,78015,78017,78016,78011,78004,78000,72852,72855,72853,72851],"class_list":["post-26243","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-dynamic-programming","category-java","tag-bitonic-array","tag-bitonic-sequence-example","tag-bitonic-sequence-in-parallel-algorithm","tag-bitonic-series","tag-bitonic-subsequence","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-bitonic-sequence","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-for-bitonic-sequence","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-java","tag-dynamic-programming-java","tag-dynamic-programming-problems","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-find-the-length-of-longest-bitonic-subsequence-in-an-array","tag-how-to-solve-dynamic-programming-problems","tag-longest-bitonic-subsequence","tag-longest-bitonic-subsequence-in-java","tag-longest-bitonic-subsequence-in-java-code","tag-longest-bitonic-subsequence-in-java-program","tag-longest-decreasing-subsequence","tag-maximum-length-bitonic-subsequence-dynamic-programming","tag-printing-longest-bitonic-subsequence","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26243"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26243\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}