Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- //Task 5: Write a program that finds the maximal increasing sequence in an array.
- //Example:{3, 2, 3, 4, 2, 2, 4} {2, 3, 4}.
- namespace Task5MaxIncreasing
- {
- class MaxIncreasing
- {
- static void Main(string[] args)
- {
- 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
- int startindex = 0;
- int length = 1;
- int maxstart = 0;
- int maxlength = 1;
- for (int i = 0; i < array.Length-1; i++)
- {
- if (array[i] < array[i + 1])
- {
- length++;
- if (length > maxlength)
- {
- maxlength = length;
- maxstart = startindex;
- }
- }
- else
- {
- length = 1;
- startindex = i + 1;
- }
- }
- for (int max = maxstart; max < maxstart + maxlength; max++)
- {
- Console.WriteLine(array[max]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement