Advertisement
minusa71

Task 5/Write a program that finds the maximal increasing seq

Jan 12th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. //Task 5: Write a program that finds the maximal increasing sequence in an array.
  6. //Example:{3, 2, 3, 4, 2, 2, 4}  {2, 3, 4}.
  7.  
  8. namespace Task5MaxIncreasing
  9. {
  10. class MaxIncreasing
  11. {
  12. static void Main(string[] args)
  13. {
  14. int[] array ={ 2, 4, 7, 5, 6, 7, 8, 5, 6, 7, 8, 9, 10, 4, 3, 1, 2, 3, 4, 5, 6 }; //there is two maximal, but code find only first
  15. int startindex = 0;
  16. int length = 1;
  17. int maxstart = 0;
  18. int maxlength = 1;
  19. for (int i = 0; i < array.Length-1; i++)
  20. {
  21. if (array[i] < array[i + 1])
  22. {
  23. length++;
  24. if (length > maxlength)
  25. {
  26. maxlength = length;
  27. maxstart = startindex;
  28. }
  29. }
  30. else
  31. {
  32. length = 1;
  33. startindex = i + 1;
  34. }
  35. }
  36. for (int max = maxstart; max < maxstart + maxlength; max++)
  37. {
  38. Console.WriteLine(array[max]);
  39. }
  40.  
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement