Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //Write a program that finds the maximal
- //increasing sequence in an array.
- //Example: {3, 2, 3, 4, 2, 2, 4} {2, 3, 4}.
- namespace _04.SequenceOfEqualElements
- {
- class SequenceOfEqualElements
- {
- static void Main(string[] args)
- {
- int[] myArray = { 0, 1, 2, 3, 4, 2, 9, 14, 15, 16, 17, 18, 30, 32, 33 };
- int counter = 1; //initiliaze counter for the sequence
- int maxSeq = 0; //max sequence
- int firstIndex = 0; //the first number of sequence
- int endIndex = 0;
- for (int i = 0; i < myArray.Length; i++)
- {
- //checks if that last element is reached if it is it should make a last check for counter>maxSeq
- if ((i != myArray.Length - 1) && myArray[i] < myArray[i + 1])
- {
- counter++;
- }
- else
- {
- if (counter > maxSeq)
- {
- maxSeq = counter;
- firstIndex = i - maxSeq + 1;
- endIndex = i;
- counter = 1;
- }
- }
- }
- //maybe we have to put it in an array but this is shorter :)
- for (int i = firstIndex; i <= endIndex; i++)
- {
- Console.Write("{0} ", myArray[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement