Advertisement
Krissy

05 Max Sequence Increasing Elements

Jan 6th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. class MaxIncreasingSequence
  8. {
  9.     static void Main()
  10.     {
  11.         Console.WriteLine("Please enter the number of elements in your array:");
  12.         int sizeOfArray = int.Parse(Console.ReadLine());
  13.         int[] Array = new int[sizeOfArray];
  14.         int currentLen = 1;
  15.         int bestLen = 1;
  16.         int bestLenIndex=0;
  17.  
  18.         for (int i = 0; i < sizeOfArray; i++)
  19.         {
  20.             Console.WriteLine("Please enter the elements for the array:");
  21.             Array[i] = int.Parse(Console.ReadLine());
  22.             if (i == 0)
  23.             {
  24.                 continue;
  25.             }
  26.             if (Array[i] == Array[i - 1]+1)
  27.             {
  28.                 currentLen++;
  29.  
  30.             }
  31.             if (i == sizeOfArray - 1)
  32.             {
  33.                 if (currentLen > bestLen)
  34.                 {
  35.                     bestLen = currentLen;
  36.                     bestLenIndex = i;
  37.                 }
  38.             }
  39.             if (Array[i] != Array[i - 1]+1  )
  40.             {
  41.                 if (currentLen > bestLen)
  42.                 {
  43.                     bestLen = currentLen;
  44.                     bestLenIndex=i-1;
  45.                 }
  46.                 currentLen = 1;
  47.             }
  48.         }
  49.         Console.WriteLine("The maximum sequence of increasing elements is:{0}", bestLen);
  50.         Console.Write("The elements are: {");
  51.  
  52.         for (int i = bestLenIndex-bestLen+1; i <= bestLenIndex; i++)
  53.         {
  54.             if (i==bestLenIndex)
  55.             {
  56.                Console.WriteLine(Array[i]+"}");
  57.             }
  58.             else
  59.             {
  60.                 Console.Write(Array[i] + ", ");
  61.             }  
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement