TheBulgarianWolf

LongestSeqOfEqualElements

Dec 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         string inputArrayOne = Console.ReadLine();
  8.         char[] delimiter = new char[]{',', ' '};
  9.         string[] inputOne = inputArrayOne.Split(delimiter,StringSplitOptions.RemoveEmptyEntries);
  10.        
  11.         int[] arr = new int[inputOne.Length];
  12.          for (int i = 0; i < inputOne.Length; i++)
  13.          {
  14.                 arr[i] = int.Parse(inputOne[i]);
  15.          }
  16.             int counter = 0;
  17.             int maxSequence = 0;
  18.             int index = 0 ;
  19.  
  20.             for (int i = 0; i < arr.Length; i++)
  21.             {
  22.                 counter = 0;
  23.                 int j = i;
  24.  
  25.                 while (arr[i] == arr[j])
  26.                 {
  27.                     counter++;
  28.                     j++;
  29.                     if (j >= arr.Length)
  30.                     {
  31.                         break;
  32.                     }
  33.                 }  
  34.  
  35.                 if (counter > maxSequence)
  36.                 {
  37.                     maxSequence = counter;
  38.                     index = i;
  39.                 }
  40.             }
  41.  
  42.             Console.WriteLine("The longest sequence is {0} elements long.", maxSequence);
  43.             for (int i = index; i <= index+maxSequence - 1 ; i++)
  44.             {
  45.                 if (i != index + maxSequence -1)
  46.                 {
  47.                     Console.Write(arr[i] + ", ");
  48.                 }
  49.                 else
  50.                 {
  51.                     Console.WriteLine(arr[i]);
  52.                 }
  53.             }
  54.         }
  55.  
  56. }
Add Comment
Please, Sign In to add comment