TheBulgarianWolf

Longest Sequence Of Equal Elements

Oct 16th, 2020
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Numerics;
  6.  
  7. namespace SoftuniExercisesWithVariables
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             Console.WriteLine("Enter your array here(add any number not connected to your array at the end.): ");
  15.             int[] array = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  16.            
  17.             int longestEqual = 0;
  18.             int currentLongest = 1;
  19.             int startIndex = 0;
  20.             int lastIndex = 0;
  21.             for (int i = 0; i < array.Length - 1; i++)
  22.             {
  23.                 if(array[i] == array[i + 1])
  24.                 {
  25.                     currentLongest++;
  26.                 }
  27.                 else
  28.                 {
  29.                     if(currentLongest > longestEqual)
  30.                     {
  31.                         longestEqual = currentLongest;
  32.                         lastIndex = i;
  33.                     }
  34.                     currentLongest = 1;
  35.                 }
  36.  
  37.                
  38.             }
  39.  
  40.             startIndex = lastIndex - longestEqual + 1;
  41.  
  42.             for(int k = startIndex; k <= lastIndex; k++)
  43.             {
  44.                 Console.Write(array[k] + " ");
  45.             }
  46.            
  47.         }
  48.     }
  49. }
  50.  
Add Comment
Please, Sign In to add comment