Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. //Write a program that finds the maximal sequence of equal elements in an array.
  2. // Example: {2, 1, 1, 2, 3, 3, 2, 2, 2, 1}  {2, 2, 2}.
  3.  
  4. using System;
  5.  
  6. class MaximalSequenceOfEqualElements
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.Write("Enter the lenght of the array : ");
  11. int lenght = int.Parse(Console.ReadLine());
  12. int[] array = new int[lenght];
  13. for (int i = 0; i < lenght; i++)
  14. {
  15. array[i] = int.Parse(Console.ReadLine());
  16. }
  17. int counter = 1; // I will use this to count the equal elements
  18. int max = 0; // I will keep the max equal elements here
  19. int element = 0;
  20. for (int i = 0; i < array.Length-1; i++)
  21. {
  22. if (array[i]==array[i+1])
  23. {
  24. counter++;
  25. }
  26. else
  27. {
  28. counter = 1;
  29. }
  30. if (max<counter)
  31. {
  32. max = counter;
  33. element = array[i];
  34. }
  35. }
  36. Console.WriteLine("The maximum sequence of equal \"{1}\" elements is ---> {0}",max,element);
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement