using System.Collections.Generic;
public static int longestIncreasingSubSequense(int[] array)
helper(array, 0, new List<int>(), ref longest);
public static void helper(int[] array, int i, List<int> slate, ref int longest)
longest = Math.Max(longest, slate.Count);
if (slate.Count == 0 || slate[slate.Count - 1] < num)
helper(array, i + 1, slate, ref longest);
slate.RemoveAt(slate.Count - 1);
helper(array, i + 1, slate, ref longest);
public static int longestIncreasingSubSequenceOptimized(int[]array){
int[] cache = new int[array.Length];
for(int i=0;i<cache.Length;i++){ cache[i]=1;}
for(int i=1;i<cache.Length;i++){
if(array[i]>array[j]&&cache[i]<=cache[j]){
max=Math.Max(max,cache[i]);
public static void Main()
Console.WriteLine(longestIncreasingSubSequense(new int[]{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}));
Console.WriteLine(longestIncreasingSubSequenceOptimized(new int[]{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}));