using System.Collections.Generic;
public static void Main()
Console.WriteLine(" Output: " + z.MaxLength(new int[]{1}));
Console.WriteLine(" Output: " + z.MaxLength(new int[]{1, 2}));
Console.WriteLine(" Output: " + z.MaxLength(new int[]{1, 2, 3, 4, 5, 6}));
Console.WriteLine(" Output: " + z.MaxLength(new int[]{1, 7, 4, 9, 2, 5}));
Console.WriteLine(" Output: " + z.MaxLength(new int[]{1,4,7,2,5}));
Console.WriteLine(" Output: " + z.MaxLength(new int[]{1,2, 7, 4, 9, 100,80,85,82,110}));
Console.WriteLine(" Output: " + z.MaxLength(new int[]{ 374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
249, 22, 176, 279, 23, 22, 617, 462, 459, 244 }));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{1}));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{1, 2}));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{1, 2, 3, 4, 5, 6}));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{1, 7, 4, 9, 2, 5}));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{1,4,7,2,5}));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{1,2, 7, 4, 9, 100,80,85,82,110}));
Console.WriteLine(" Output: " + z.MaxLength2(new int[]{ 374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
249, 22, 176, 279, 23, 22, 617, 462, 459, 244 }));
public int MaxLength2(int[] a)
Stack<int> up = new Stack<int>();
Stack<int> down = new Stack<int>();
if(down.Count() == 0 && a[i] >= a[i-1])
else if(up.Count == 0 && a[i] <= a[i-1])
public int MaxLength(int[] a)
List<int> up = new List<int>();
List<int> down = new List<int>();
up[i] = Math.Max(down[j] +1 , up[i]);
down[i] = Math.Max(up[j]+1, down[i]);
maxLength = Math.Max(maxLength, Math.Max(up[i], down[i]));
private void Print(string s, List<int> list)
foreach(var item in list)
Console.Write(item + " ");
private void Print(string s, int[] a)
public int[] CreateTemp(int[] a)
t[n-1] = t[n-2] > 0 ? a[n-1] *-1: a[n-1];