Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- * 5. Write a program that finds the maximal increasing sequence in an array.
- * Example: {3, 2, 3, 4, 2, 2, 4} -> {2, 3, 4}
- */
- class MaximalIncreasingSequence
- {
- static void Main()
- {
- int[] arr = { 3, 2, 3, 4, 5, 2, 4 };
- int start = arr[0];
- int endIndex = arr[0];
- int startInt = arr[0];
- int length = 1;
- int bestLength = length;
- for (int i = 0; i < arr.Length; i++)
- {
- if (i != arr.Length - 1)
- {
- if (arr[i] < arr[i + 1])
- {
- length++;
- if (length > bestLength)
- {
- bestLength = length;
- endIndex = i + 1;
- }
- }
- else
- {
- length = 0;
- }
- }
- else if (arr[i - 1] < arr[i])
- {
- bestLength++;
- }
- }
- Console.WriteLine("{ 3, 2, 3, 4, 5, 2, 4 }");
- Console.WriteLine("The maximal increasing sequence of elements is: ");
- Console.Write("{ ");
- for (int i = endIndex + 1 - bestLength; i <= endIndex; i++)
- {
- if (i == endIndex)
- {
- Console.Write("{0}", arr[i]);
- }
- else
- {
- Console.Write("{0}, ", arr[i]);
- }
- }
- Console.WriteLine(" }");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement