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