Advertisement
Martichka

Arrays/ Task - 5 Maximal Increas

Jan 13th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.95 KB | None | 0 0
  1. using System;
  2. class MaximalIncreas
  3. {
  4.     static void Main()
  5.     {
  6.         int[] mainArray = new int[] { 1, 2, 5, 5, 3, 1, 2, 3, 4, 6, 2 };
  7.         int length = 1;
  8.         int count = 1;
  9.         string arrayNums = mainArray[0].ToString() + " ";
  10.         string longestSequence = "";
  11.  
  12.         for (int start = 0; start < mainArray.Length-1; start++)
  13.         {
  14.             if (mainArray[start] == mainArray[start+1]-1)
  15.             {
  16.                 count += 1;
  17.                 arrayNums += mainArray[start + 1] + " ";
  18.             }
  19.             if (count > length)
  20.             {
  21.                 length = count;
  22.                 longestSequence = arrayNums;
  23.             }
  24.             else if (mainArray[start] != mainArray[start + 1]-1)
  25.             {
  26.                 count = 1;
  27.                 arrayNums = mainArray[start + 1] + " ";
  28.             }
  29.         }
  30.         Console.WriteLine(length);
  31.         Console.WriteLine(longestSequence);
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement