Advertisement
Klaxon

[C# Arrays] Maximal Increasing Sequence

Jul 15th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.67 KB | None | 0 0
  1. // 5. Write a program that finds the maximal increasing sequence in an array.
  2. //    Example: {3, 2, 3, 4, 2, 2, 4} -> {2, 3, 4}.
  3.  
  4. using System;
  5.  
  6. class MaximalIncreasingSequence
  7. {
  8.     static void Main()
  9.     {
  10.         // Make an array and get the size
  11.         int length = int.Parse(Console.ReadLine());
  12.         int[] array = new int[length];
  13.  
  14.         // Help variables
  15.         int len = 1;
  16.         int bestLen = 0;
  17.         int startLen = 0;
  18.  
  19.         // Loop to fill up the array
  20.         for (int i = 0; i < length; i++)
  21.         {
  22.             array[i] = int.Parse(Console.ReadLine());
  23.  
  24.             // Check is the array is filled up
  25.             if (i == length - 1)
  26.             {
  27.                 // Loop for comparing every number with the next
  28.                 for (int j = 0; j < i; j++)
  29.                 {
  30.                     if ((array[j] + 1) == (array[j + 1]))
  31.                     {
  32.                         len++;
  33.                     }
  34.                     else
  35.                     {
  36.                         len = 1;
  37.                     }
  38.                     if (len > bestLen)
  39.                     {
  40.                         bestLen = len;
  41.                         startLen = j; // Get the index of the last number
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.  
  47.         // Printing
  48.         Console.Write("{");
  49.         for (int i = startLen - bestLen + 1; i < startLen + 1; i++)
  50.         {
  51.             if (i == startLen)
  52.             {
  53.                 Console.Write(array[i + 1]);
  54.             }
  55.             else
  56.             {
  57.                 Console.Write("{0}, ", array[i + 1]);
  58.             }
  59.         }
  60.         Console.Write("}");
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement