Advertisement
Guest User

07. Max Sequence of Increasing Elements

a guest
Oct 11th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Max_Sequence_of_Equal_Elements
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var input = Console.ReadLine()
  14.                 .Split(' ')
  15.                 .Select(int.Parse)
  16.                 .ToArray();
  17.             int startPos = 0;
  18.             int lenOfIncreasing = 1;
  19.             int bestStartPos = 0;
  20.             int bestLen = 1;
  21.             for (int i = 1; i < input.Length; i++)
  22.             {
  23.                 if (input[i] == input[i - 1] + 1)
  24.                 {
  25.                     lenOfIncreasing++;
  26.                     if (lenOfIncreasing > bestLen)
  27.                     {
  28.                         bestStartPos = startPos;
  29.                         bestLen = lenOfIncreasing;
  30.                     }
  31.                 }
  32.                 else
  33.                 {
  34.                     startPos = i;
  35.                     lenOfIncreasing = 1;
  36.                 }
  37.             }
  38.             for (int i = bestStartPos; i < bestStartPos + bestLen; i++)
  39.             {
  40.                 Console.Write(input[i] + " ");
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement