Advertisement
TanyaPetkova

C# Part II Arrays Exercise 5

Dec 21st, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. //Write a program that finds the maximal increasing sequence in an array.
  2. //Example: {3, 2, 3, 4, 2, 2, 4} -> {2, 3, 4}.
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. class MaximalIncreasingSequence
  8. {
  9.     static void Main()
  10.     {
  11.         int arrayLength;
  12.         int currentLength = 1;
  13.         int maxLength = 0;
  14.         List<int> indices = new List<int>();
  15.  
  16.         Console.Write("Enter the array's length: ");
  17.  
  18.         while (!int.TryParse(Console.ReadLine(), out arrayLength) || arrayLength <= 0)
  19.         {
  20.             Console.Write("Invalid input. Enter a positive integer number: ");
  21.         }
  22.  
  23.         int[] array = new int[arrayLength];
  24.  
  25.         for (int i = 0; i < arrayLength; i++)
  26.         {
  27.             Console.Write("Enter the {0} element of the array: ", i);
  28.  
  29.             while (!int.TryParse(Console.ReadLine(), out array[i]))
  30.             {
  31.                 Console.Write("Invalid input. Enter an integer number: ");
  32.             }
  33.         }
  34.  
  35.         int pointer = array[0];
  36.  
  37.         for (int i = 1; i < arrayLength; i++)
  38.         {
  39.             if (pointer < array[i])
  40.             {
  41.                 pointer = array[i];
  42.                 currentLength++;
  43.             }
  44.             else
  45.             {
  46.                 pointer = array[i];
  47.  
  48.                 if (maxLength < currentLength)
  49.                 {
  50.                     maxLength = currentLength;
  51.                     indices.Clear();
  52.                     indices.Add(i - 1);
  53.                 }
  54.                 else if (maxLength == currentLength)
  55.                 {
  56.                     indices.Add(i - 1);
  57.                 }
  58.  
  59.                 currentLength = 1;
  60.             }
  61.         }
  62.  
  63.         if (maxLength < currentLength)
  64.         {
  65.             maxLength = currentLength;
  66.             indices.Clear();
  67.             indices.Add(arrayLength - 1);
  68.         }
  69.         else if (maxLength == currentLength)
  70.         {
  71.             indices.Add(arrayLength - 1);
  72.         }
  73.  
  74.         if (indices.Count == 1)
  75.         {
  76.             Console.Write("There is one maximal increasing sequence: ");
  77.         }
  78.         else
  79.         {
  80.             Console.WriteLine("There are {0} maximal increasing sequences: ", indices.Count);
  81.         }
  82.  
  83.         for (int i = 0; i < indices.Count; i++)
  84.         {
  85.             int[] maxSequence = new int[maxLength];
  86.  
  87.             for (int j = maxLength - 1; j >= 0; j--)
  88.             {
  89.                 maxSequence[j] = array[indices[i]];
  90.                 indices[i]--;
  91.             }
  92.  
  93.             Console.WriteLine(string.Join(", ", maxSequence));
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement