aleahim

Arrays_problem04

Jan 25th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class FindMaxSeq_04
  5. {
  6.     static void Main()
  7.     {
  8.         int n = int.Parse(Console.ReadLine());
  9.         int[] arr = new int[n];
  10.         for (int i = 0; i < n; i++)
  11.         {
  12.             arr[i] = int.Parse(Console.ReadLine());
  13.         }
  14.         List<int> listSeq = new List<int>();
  15.  
  16.         int seqLen = 1;
  17.         int bestLen = 1;
  18.  
  19.         for (int i = 0; i < n-1; i++)
  20.         {
  21.             if (arr[i] != arr[i + 1]) seqLen = 1;
  22.             else
  23.             {
  24.                 seqLen++;
  25.                 if (seqLen > bestLen)
  26.                 {
  27.                     bestLen = seqLen;
  28.                     listSeq.Clear();
  29.                     listSeq.Add(arr[i + 1]);
  30.                 }
  31.                 else if (seqLen == bestLen)
  32.                 {
  33.                     listSeq.Add(arr[i + 1]);
  34.                 }
  35.             }
  36.         }
  37.         int[] sequence = listSeq.ToArray();
  38.         int seqLength = sequence.Length;
  39.         string result = "";
  40.  
  41.         for (int m = 0; m < seqLength; m++)
  42.         {
  43.             for (int j = 0; j < bestLen; j++)
  44.             {
  45.                 result += sequence[m] + " ";
  46.             }
  47.         }
  48.         if (bestLen <= 1)
  49.         {
  50.             Console.WriteLine("No sequence found!");
  51.         }
  52.         else Console.WriteLine("The maximal sequence of equal elements is " + bestLen + ":" + result);
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment