Advertisement
CuST0M1z3

4. MaxSequenceOfEqualElements

Jun 30th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class MaxSequenceOfEqualElements
  9. {
  10.     static void Main()
  11.     {
  12.         int[] arr = { 2, 2, 1, 1, 2, 3, 3, 3, 2, 2, 2, 4, 4, 4 };
  13.  
  14.         List<int> m = new List<int>();
  15.  
  16.         int len = 1;
  17.         int maxLen = 0;
  18.         int longestNumber = 0;
  19.         int length = arr.Length;
  20.  
  21.         for (int i = 0; i < arr.Length - 1; i++)
  22.         {
  23.             if (arr[i] == arr[i + 1])
  24.             {
  25.                 len++;
  26.                 if (len > maxLen)
  27.                 {
  28.                     maxLen = len;
  29.                 }
  30.             }
  31.             else
  32.             {
  33.                 len = 1;
  34.             }            
  35.         }
  36.  
  37.         len = 1;
  38.  
  39.         for (int i = 0; i < arr.Length - 1; i++)
  40.         {
  41.             if (arr[i] == arr[i+1])
  42.             {
  43.                 len++;
  44.                 if (len == maxLen)
  45.                 {
  46.                     longestNumber = arr[i];
  47.                     m.Add(longestNumber);
  48.                 }    
  49.             }
  50.             else
  51.             {              
  52.                 len = 1;
  53.             }            
  54.         }
  55.  
  56.         foreach (var num in m)
  57.         {
  58.             for (int i = 0; i < maxLen; i++)
  59.             {
  60.                 Console.Write(num + " ");
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement