Advertisement
Teodor92

05. MaxIncr

Jan 6th, 2013
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. /* Write a program that finds the maximal
  2.  * increasing sequence in an array. Example:
  3.  * {3, 2, 3, 4, 2, 2, 4}  {2, 3, 4}.
  4.  */
  5.  
  6. using System;
  7.  
  8. class MaximalIncreasingSequence
  9. {
  10.     static void Main()
  11.     {
  12.         int[] myArray = {0, 1, 2, 3, 4, 2, 3, 4, 5, 6, 7, 8, 30, 2, 3};
  13.         int len = 1;
  14.         int bestLen = 0;
  15.         int endIndex = 0;
  16.         // main logic
  17.         for (int i = 0; i < myArray.Length-1; i++)
  18.         {
  19.             if (myArray[i] < myArray[i+1])
  20.             {
  21.                 len++;
  22.             }
  23.             else
  24.             {
  25.                 if (len > bestLen)
  26.                 {
  27.                     bestLen = len;
  28.                     endIndex = i;
  29.                 }
  30.                 len = 1;
  31.             }
  32.         }
  33.         // checking a special last case
  34.         if (len > bestLen)
  35.         {
  36.             bestLen = len;
  37.             endIndex = myArray.Length-1;
  38.         }
  39.         // output
  40.         len = 1;
  41.         Console.WriteLine("The longest sequence of increasing elemints is:");
  42.         Console.Write("{");
  43.         for (int i = endIndex - bestLen + 1; i < endIndex + 1; i++)
  44.         {
  45.             Console.Write("{0},", myArray[i]);
  46.         }
  47.         Console.WriteLine("}");
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement