Advertisement
cortez

Find Maximal Increasing Sequence In Array

Jun 30th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MaxIncreasingSequence
  4. {
  5.     class MaxIncreasingSequence
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.WriteLine("Please enter length for the array:");
  10.             int n = int.Parse(Console.ReadLine());
  11.             int start = 0;
  12.             int len = 1;
  13.             int bestLen = 0;
  14.             string num = "";
  15.             string maxIncSeq = "";
  16.             int[] numbers = new int[n];
  17.             int length = numbers.Length;
  18.  
  19.             for (int i = 0; i < length; i++)
  20.             {
  21.                 Console.Write("Enter {0} number: ", i + 1);
  22.                 numbers[i] = int.Parse(Console.ReadLine());
  23.             }
  24.  
  25.             for (int i = 1; i < length; i++)
  26.             {
  27.                 if (numbers[i] - numbers[i - 1] == 1)
  28.                 {
  29.                     start++;
  30.                     if (start == len)
  31.                     {
  32.                         num += numbers[i - 1];
  33.                         num += numbers[i];
  34.                     }
  35.                     if (start > len)
  36.                     {
  37.                         len = start;
  38.                         num += numbers[i];
  39.                     }
  40.                     if (len > bestLen)
  41.                     {
  42.                         bestLen = len;
  43.                         maxIncSeq = num;
  44.                     }
  45.                 }
  46.                 else
  47.                 {
  48.                     num = "";
  49.                     start = 0;
  50.                     len = 1;
  51.                 }
  52.             }
  53.             Console.WriteLine("Max increasing sequence: {0}", maxIncSeq);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement