Advertisement
vlad0

Homework Arrays - 5

Jan 10th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. //Write a program that finds the maximal
  8. //increasing sequence in an array.
  9. //Example: {3, 2, 3, 4, 2, 2, 4}  {2, 3, 4}.
  10.  
  11.  
  12.  
  13. namespace _04.SequenceOfEqualElements
  14. {
  15.     class SequenceOfEqualElements
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             int[] myArray = { 0, 1, 2, 3, 4, 2, 9, 14, 15, 16, 17, 18, 30, 32, 33 };
  20.             int counter = 1; //initiliaze counter for the sequence
  21.             int maxSeq = 0; //max sequence
  22.             int firstIndex = 0; //the first number of sequence
  23.             int endIndex = 0;
  24.  
  25.  
  26.             for (int i = 0; i < myArray.Length; i++)
  27.             {
  28.                 //checks if that last element is reached if it is it should make a last check for counter>maxSeq
  29.                 if ((i != myArray.Length - 1) && myArray[i] < myArray[i + 1])
  30.                 {
  31.                     counter++;
  32.                 }
  33.                 else
  34.                 {
  35.                     if (counter > maxSeq)
  36.                     {
  37.                         maxSeq = counter;
  38.                         firstIndex = i - maxSeq + 1;
  39.                         endIndex = i;
  40.                         counter = 1;
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             //maybe we have to put it in an array but this is shorter :)
  46.             for (int i = firstIndex; i <= endIndex; i++)
  47.             {
  48.                 Console.Write("{0} ", myArray[i]);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement