Advertisement
pavlinpetkov88

Max Sequence of Equal Elements

Feb 6th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace MaxSequence
  6. {
  7. public class MaxSequence
  8. {
  9. static void Main(string[] args)
  10. {
  11. int[] numbers = Console.ReadLine().Split(' ')
  12. .Select(int.Parse)
  13. .ToArray();
  14.  
  15. FindLongestSequence(numbers);
  16.  
  17. }
  18.  
  19. private static void FindLongestSequence(int[] array)
  20. {
  21. int start = 0;
  22. int len = 1;
  23.  
  24. int bestPosition = 0;
  25. int bestLen = 1;
  26.  
  27. for (int i = 1; i < array.Length; i++)
  28. {
  29. if (array[i] == array[i - 1])
  30. {
  31. len++;
  32. if (len > bestLen)
  33. {
  34. bestLen = len;
  35. bestPosition = start;
  36. }
  37. }
  38. else
  39. {
  40. if (len > bestLen)
  41. {
  42. bestPosition = start;
  43. bestLen = len;
  44. }
  45. start = i;
  46. len = 1;
  47. }
  48. }
  49.  
  50. for (int i = bestPosition; i < bestPosition + bestLen; i++)
  51. {
  52. Console.Write($"{array[i]} ");
  53. }
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement