Advertisement
ralka

07. Max Sequence of Increasing Elements

May 27th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 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 _06.Max_Sequence_of_Equal_Elements
  8. {
  9.     class MaxSequenceOfEqualElements
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             long[] numbers = Console.ReadLine()
  14.                 .Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries)
  15.                 .Select(long.Parse)
  16.                 .ToArray();
  17.  
  18.             int maxCount = 0;
  19.             int startIndex = 0;
  20.             int count = 0;
  21.             int index = 0;
  22.  
  23.             for (int i = 1; i < numbers.Length; i++)
  24.             {
  25.                 if (numbers[i - 1] + 1 == numbers[i])
  26.                 {
  27.                     count++;
  28.                     index = i - count;
  29.                     if (count > maxCount)
  30.                     {
  31.                         maxCount = count;
  32.                         startIndex = index;
  33.                     }
  34.                 }
  35.                 else
  36.                 {
  37.                     count = 0;
  38.                 }
  39.             }
  40.  
  41.  
  42.             PrintLongestSequence(numbers, startIndex, maxCount);
  43.  
  44.         }
  45.  
  46.         private static void PrintLongestSequence(long[] numbers, int index, int count)
  47.         {
  48.             for (int i = index; i <= index + count; i++)
  49.             {
  50.                 Console.Write($"{numbers[i]} ");
  51.             }
  52.             Console.WriteLine();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement