Advertisement
fbinnzhivko

09. Largest Increasing

Jun 5th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         var longestLength = 0;
  10.         var longestSequence = new List<int>();
  11.  
  12.         var arr = Console.ReadLine().Split().Select(int.Parse).ToList();
  13.      
  14.         for (int i = 0; i < arr.Count; i++)
  15.         {
  16.             var sequence = checkSequence(arr, i);
  17.             if (sequence.Count > longestLength)
  18.             {
  19.                 longestLength = sequence.Count;
  20.                 longestSequence = sequence;
  21.             }
  22.         }
  23.         if (arr.Count == 1)
  24.         {
  25.             Console.WriteLine(arr[0]);
  26.         }
  27.         else
  28.         {
  29.             Console.WriteLine(string.Join(" ", longestSequence));
  30.         }
  31.  
  32.        
  33.     }
  34.  
  35.     private static List<int> checkSequence(List<int> arr, int startPosition)
  36.     {
  37.         List<int> lis = new List<int>();
  38.  
  39.         var minNumber = arr[startPosition];
  40.         var indexOfMinNumber = arr.IndexOf(minNumber);
  41.         lis.Add(minNumber);
  42.  
  43.         for (int i = ++indexOfMinNumber; i < arr.Count; i++)
  44.         {
  45.             if (arr[i] > minNumber)
  46.             {
  47.                 if (checkIfThereIsLowerNumber(arr, i, minNumber))
  48.                 {
  49.                     continue;
  50.                 }
  51.                 else
  52.                 {
  53.                     lis.Add(arr[i]);
  54.                     minNumber = arr[i];
  55.                 }
  56.             }
  57.             else
  58.             {
  59.                 continue;
  60.             }
  61.         }
  62.  
  63.         return lis;
  64.     }
  65.  
  66.     private static bool checkIfThereIsLowerNumber(List<int> arr, int position, int minNumber)
  67.     {
  68.         for (var i = position; i < arr.Count - 1; i++)
  69.         {
  70.             if (arr[position] > arr[i + 1] && arr[i + 1] > minNumber)
  71.             {
  72.                 return true;
  73.             }
  74.         }
  75.  
  76.         return false;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement