Advertisement
dimipan80

Longest Increasing Sequence

May 7th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. /* Write a program to find all increasing sequences inside an array of integers.
  2.  * The integers are given on a single line, separated by a space.
  3.  * Print the sequences in the order of their appearance in the input array,
  4.  * each at a single line. Separate the sequence elements by a space.
  5.  * Find also the longest increasing sequence and print it at the last line.
  6.  * If several sequences have the same longest length, print the left-most of them.
  7.  */
  8.  
  9. namespace _05.LongestIncreasingSequence
  10. {
  11.     using System;
  12.     using System.Linq;
  13.  
  14.     class LongestIncreasingSequence
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             Console.WriteLine("Enter your Integer numbers on single line, separated by a space: ");
  19.             int[] numbers = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  20.  
  21.             int longStart = 0;
  22.             int start = 0;
  23.             int longEnd = 0;
  24.             int longLength = 0;
  25.             int length = 0;
  26.             for (int i = 0; i < numbers.Length - 1; i++)
  27.             {
  28.                 Console.Write(numbers[i]);
  29.                 if (numbers[i] < numbers[i + 1])
  30.                 {
  31.                     Console.Write(" ");
  32.                     length++;
  33.                 }
  34.                 else
  35.                 {
  36.                     Console.WriteLine();
  37.                     if (length > longLength)
  38.                     {
  39.                         longLength = length;
  40.                         longStart = start;
  41.                         longEnd = i;
  42.                     }
  43.                     length = 0;
  44.                     start = i + 1;
  45.                 }
  46.             }
  47.  
  48.             Console.WriteLine(numbers[numbers.Length - 1]);
  49.             if (length > longLength)
  50.             {
  51.                 longLength = length;
  52.                 longStart = start;
  53.                 longEnd = numbers.Length - 1;
  54.             }
  55.  
  56.             Console.Write("Longest:");
  57.             for (int i = longStart; i <= longEnd; i++)
  58.             {
  59.                 Console.Write(" {0}", numbers[i]);
  60.             }
  61.             Console.WriteLine();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement