Advertisement
Razhagal

Longest Area in Array

Mar 31st, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 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. class LongestAreaInArray
  8. {
  9.     static void Main()
  10.     {
  11.         int arrayLenght = int.Parse(Console.ReadLine());
  12.         string[] words = new string[arrayLenght];
  13.  
  14.         for (int i = 0; i < words.Length; i++)
  15.         {
  16.             string arrayElement = Console.ReadLine();
  17.             words[i] = arrayElement;
  18.         }
  19.  
  20.         int startIndex = 0;
  21.         int lenghtCount = 1;
  22.         int currentCount = 1;
  23.  
  24.         for (int i = 0; i < words.Length - 1; i++) //could start on index 1 and check current with previous elements
  25.         {
  26.             if (words[i] == words[i + 1])
  27.             {
  28.                 currentCount++;
  29.  
  30.                 if (currentCount > lenghtCount)
  31.                 {
  32.                     lenghtCount = currentCount;
  33.                     startIndex = (i + 1) - (lenghtCount - 1);
  34.                 }
  35.             }
  36.             else
  37.             {
  38.                 currentCount = 1;
  39.             }
  40.         }
  41.  
  42.         Console.WriteLine(lenghtCount);
  43.         for (int i = 0; i < lenghtCount; i++)
  44.         {
  45.             Console.WriteLine(words[startIndex + i]);
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement