2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5e6dd139859c290b6ab80292
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Longest increasing subsequence
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2020-06-08 20:07:41 +05:30 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 385272
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: longest-increasing-subsequence
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								The longest 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. An example:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								For the following array:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$\\{3, 10, 2, 1, 20\\}$
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Longest increasing sequence is:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								$\\{3, 10, 20\\}$
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For more information on this problem please see [Wikipedia ](<https://en.wikipedia.org/wiki/Longest increasing subsequence> ).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Write a function that takes an array of numbers as a parameter and returns the longest increasing subsequence.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								It is guaranteed that every array will have a longest increasing subsequence.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`findSequence`  should be a function. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof findSequence == 'function');
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`findSequence([3, 10, 2, 1, 20])`  should return a array. 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(Array.isArray(findSequence([3, 10, 2, 1, 20])));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`findSequence([3, 10, 2, 1, 20])`  should return `[3, 10, 20]` . 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.deepEqual(findSequence([3, 10, 2, 1, 20]), [3, 10, 20]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`findSequence([2, 7, 3, 5, 8])`  should return `[2, 3, 5, 8]` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(findSequence([2, 7, 3, 5, 8]), [2, 3, 5, 8]);
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`findSequence([2, 6, 4, 5, 1])`  should return `[2, 4, 5]` . 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(findSequence([2, 6, 4, 5, 1]), [2, 4, 5]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`findSequence([10, 22, 9, 33, 21, 50, 60, 80])`  should return `[10, 22, 33, 50, 60, 80]` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(findSequence([10, 22, 9, 33, 21, 50, 60, 80]), [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  10,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  22,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  33,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  50,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  60,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  80
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`findSequence([0, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15])`  should return `[0, 2, 6, 9, 11, 15` . 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  findSequence([0, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [0, 2, 6, 9, 11, 15]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function findSequence(input) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2020-04-21 09:50:40 +05:30 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function findSequence(input) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var len = input.length;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var result = []
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (var i = 0; i <  len ;  i + + )  result . push ( 1 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (var i = 0; i <  len ;  i + + ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        for (var j = i - 1; j >= 0; j--)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            if (input[i] > input[j] & &  result[j] >= result[i])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								                result[i] = result[j] + 1;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var maxValue = Math.max.apply(null, result);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var maxIndex = result.indexOf(Math.max.apply(Math, result));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    var output = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    output.push(input[maxIndex]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    for (var i = maxIndex; i >= 0; i--) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (maxValue == 0) break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (input[maxIndex] > input[i] & &  result[i] == maxValue - 1) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            output.push(input[i]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            maxValue--;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    output.reverse();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return output;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```