Advertisement
Guest User

Untitled

a guest
Sep 4th, 2016
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _07.MaxSequenceIncreasingElements
  5. {
  6.     /*
  7.     * Write a program that finds the maximal increasing sequence in an array.
  8.     * You can check this code here:
  9.     * https://judge.softuni.bg/Contests/Practice/Index/207#6
  10.     */
  11.     class MaxSeqIncreasing
  12.     {
  13.         public static void Main()
  14.         {
  15.             var sequence = Console.ReadLine().Split().Select(long.Parse).ToArray();
  16.             FindLongestIncreasingSubsequence(sequence.ToArray(), sequence.Length);
  17.         }
  18.  
  19.         static void FindLongestIncreasingSubsequence(long[] numbers, int length)
  20.         {
  21.             int cntCurrSeq = 0;
  22.             int startCurrSeq = 0;
  23.             int cntMaxSeq = 0;
  24.             int startMaxSeq = 0;
  25.  
  26.             for (int i = 1; i < length; i++)
  27.             {
  28.                 if (numbers[i] - numbers[i - 1] >= 1)
  29.                 {
  30.                     cntCurrSeq++;
  31.                     startCurrSeq = i - cntCurrSeq;
  32.  
  33.                     if (cntCurrSeq > cntMaxSeq)
  34.                     {
  35.                         cntMaxSeq = cntCurrSeq;
  36.                         startMaxSeq = startCurrSeq;
  37.                     }
  38.                 }
  39.                 else
  40.                 {
  41.                     cntCurrSeq = 0;
  42.                 }
  43.             }
  44.             for (int iWrite = startMaxSeq; iWrite <= (startMaxSeq + cntMaxSeq); iWrite++)
  45.             {
  46.                 Console.Write(numbers[iWrite] + " ");
  47.             }
  48.             Console.WriteLine();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement