Advertisement
georgivorobyov

Biggest Increment Sequence

Jan 5th, 2013
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BiggestIncrementSequence
  5. {
  6.     class BiggestIncrementSequence
  7.     {
  8.         static void PrintResult(int[] result)
  9.         {
  10.             Console.Write("{");
  11.             for (int i = result.Length - 1; i > 0; i--)
  12.             {
  13.                 Console.Write("{0}, ", result[i]);
  14.             }
  15.             Console.WriteLine("{0}}}", result[0]);
  16.         }
  17.  
  18.         static bool AllElementsAreEqual(int[] numbers)
  19.         {
  20.             bool areEqual = true;
  21.             for (int index = 0, length = numbers.Length; index < length; index++)
  22.             {
  23.                 if (numbers[0] != numbers[index])
  24.                 {
  25.                     areEqual = false;
  26.                     break;
  27.                 }
  28.             }
  29.             return areEqual;
  30.         }
  31.  
  32.         static int[] FindBiggestIncrementSequence(int[] numbers)
  33.         {
  34.             List<int> lengths = new List<int>(new int[numbers.Length]); //the length of subelements
  35.             int[] indexs = new int[numbers.Length]; //parent index
  36.             int[] result; //The array that will contain our result
  37.             lengths[0] = 1; //first number -> length = 1
  38.             indexs[0] = -1; //number less than 0
  39.  
  40.             if (AllElementsAreEqual(numbers))
  41.             {
  42.                 return new int[1];
  43.             }
  44.  
  45.             for (int numberIndex = 1, length = numbers.Length; numberIndex < length; numberIndex++)
  46.             {
  47.                 for (int parentIndex = numberIndex - 1; parentIndex >= 0; parentIndex--)
  48.                 {
  49.                     if (numbers[numberIndex] >= numbers[parentIndex])
  50.                     {
  51.                         if (lengths[numberIndex] <= lengths[parentIndex])
  52.                         {
  53.                             lengths[numberIndex] = lengths[parentIndex] + 1;
  54.                             indexs[numberIndex] = parentIndex;
  55.                         }
  56.                     }
  57.                     else
  58.                     {
  59.                         if (lengths[numberIndex] == 0)
  60.                         {
  61.                             lengths[numberIndex] = 1;
  62.                             indexs[numberIndex] = -1;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.            
  68.             do
  69.             {
  70.                 //if the length is 1 => There were no increasing sequences found in the array.
  71.                 if (lengths.Count == 1)
  72.                 {
  73.                     return new int[1];
  74.                 }
  75.                 int resultLength = 0;
  76.                 int maxLengthIndex = 0;
  77.  
  78.                 for (int index = 0; index < lengths.Count; index++)
  79.                 {
  80.                     if (lengths[index] > resultLength)
  81.                     {
  82.                         resultLength = lengths[index];
  83.                         maxLengthIndex = index;
  84.                     }
  85.                 }
  86.                 //Probably our result :D
  87.                 int currentIndex = maxLengthIndex;
  88.                 result = new int[resultLength];
  89.                 for (int count = 0; currentIndex != -1; count++)
  90.                 {
  91.                     result[count] = numbers[currentIndex];
  92.                     currentIndex = indexs[currentIndex];
  93.                 }
  94.                 //Check for increment sequence
  95.                 if (result[0] == result[resultLength - 1])
  96.                 {
  97.                     lengths.RemoveAt(maxLengthIndex);
  98.                     continue;
  99.                 }
  100.                 break;
  101.             } while (true); //while our result is not increment sequence
  102.            
  103.             return result;
  104.         }
  105.         static void Main()
  106.         {
  107.             // TESTS:
  108.             //{ 0, 6, 1, 4, 3, 0, 3, 6, 4, 5 } -> 0, 1, 3, 3, 4, 5
  109.             //{ 1, 1, 1 } or { 1, 1, 0, 0, -1, -1 } -> there are zero increment sequences
  110.             //{ 1, 2, 3, 1, 1, 1, 1, 1 } -> 1, 2 ,3
  111.             //{ 0, 0, 0, 0, 0, 0, 3, 6, 4, 5 } -> 0, 0, 0, 0, 0, 0, 3, 4, 5
  112.             Console.Write("Please enter the length of your array: ");
  113.             int size = int.Parse(Console.ReadLine());
  114.             int[] numbers = new int[size];
  115.             for (int index = 0; index < size; index++)
  116.             {
  117.                 Console.Write("Index [{0}]: ", index);
  118.                 numbers[index] = int.Parse(Console.ReadLine());
  119.             }
  120.             int[] result = FindBiggestIncrementSequence(numbers);
  121.             //Let's print our result
  122.             if (result.Length > 1)
  123.             {
  124.                 PrintResult(result);
  125.             }
  126.             else
  127.             {
  128.                 Console.WriteLine("There were no increasing sequences found in the given array.");
  129.             }
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement