Advertisement
miroLLL

07Problem-MaxSequenceOfIncreasingElements

Feb 11th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _07Problem_MaxSequenceOfIncreasingElements
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] inputNumbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  11.  
  12.             int currentLength = 0;
  13.  
  14.             int startPosition = 0;
  15.             int bestLength = 0;
  16.  
  17.             int endOfLoop = inputNumbers.Length - 2;
  18.  
  19.             for (int i = 0; i <= endOfLoop; i++)
  20.             {
  21.                 if (inputNumbers[i] <= inputNumbers[i + 1] - 1)
  22.                 {
  23.                     currentLength++;
  24.                 }
  25.                 else
  26.                 {
  27.                     if (currentLength > bestLength)
  28.                     {
  29.                         bestLength = currentLength;
  30.                         startPosition = i - bestLength;
  31.                     }
  32.                     currentLength = 0;
  33.                 }
  34.  
  35.                 if (i == inputNumbers.Length - 2)
  36.                 {
  37.                     if (currentLength > bestLength)
  38.                     {
  39.                         startPosition = i + 1 - currentLength;
  40.                         bestLength = currentLength;
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             int endPosition = startPosition + bestLength;
  46.  
  47.             for (int i = startPosition; i <= endPosition; i++)
  48.             {
  49.                 Console.Write(inputNumbers[i] + " ");
  50.             }
  51.             Console.WriteLine();
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement