Advertisement
Guest User

07. Max Sequence of Increasing Elements

a guest
Oct 10th, 2017
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class MaxSequenceOfIncreasingElements
  5. {
  6.     static void Main()
  7.     {
  8.         int[] inputArray = Console.ReadLine()
  9.             .Trim()
  10.             .Split(new string[] { " " }, StringSplitOptions
  11.             .RemoveEmptyEntries)
  12.             .Select(int.Parse)
  13.             .ToArray();
  14.  
  15.         if (inputArray.Length < 1)
  16.         {
  17.             return;
  18.         }
  19.         else if (inputArray.Length == 1)
  20.         {
  21.             Console.WriteLine(inputArray[0]);
  22.             return;
  23.         }
  24.  
  25.         int length = 1;
  26.         int maxLength = 1;
  27.         int endIndex = 0;
  28.         int maxEndIndex = 0;
  29.  
  30.         for (int i = 0; i < inputArray.Length - 1; i++)
  31.         {
  32.             if (inputArray[i] + 1 == inputArray[i + 1])
  33.             {
  34.                 length++;
  35.                 endIndex = i + 1;
  36.             }
  37.             else
  38.             {
  39.                 if (length > maxLength)
  40.                 {
  41.                     maxEndIndex = i;
  42.                     maxLength = length;
  43.                 }
  44.  
  45.                 length = 1;
  46.             }
  47.         }
  48.  
  49.         if (length > maxLength)
  50.         {
  51.             maxEndIndex = endIndex;
  52.             maxLength = length;
  53.         }
  54.  
  55.         for (int i = maxEndIndex - maxLength + 1; i <= maxEndIndex; i++)
  56.         {
  57.             Console.Write(inputArray[i] + " ");
  58.         }
  59.  
  60.         Console.WriteLine();
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement