Advertisement
ekostadinov

Zig Zag Sequence

Nov 21st, 2012
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ZigAndZag
  8. {
  9.     class ZigZag
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.WriteLine("Enter how many number you want in your sequence (0 <= number <= 50)");
  14.             byte N = byte.Parse(Console.ReadLine());
  15.                        
  16.             int [] arr = new int [N];
  17.  
  18.             for (int index = 0; index < N; index++) //read numbers - fill array
  19.             {
  20.                 Console.WriteLine("enter {0} number: ", index); //can check the next number with while loop
  21.                 arr[index] = byte.Parse(Console.ReadLine());    //using the putBigger - putSmaller logic            
  22.             }
  23.  
  24.            
  25.             for (int index = 0; index < N; index++ ) //print numbers in array
  26.             {
  27.                 Console.Write(arr[index] + " ");
  28.                
  29.             }
  30.             Console.WriteLine();
  31.            
  32.             int len = 0;
  33.             int bestLen = len;
  34.            
  35.             int length = arr.Length;
  36.  
  37.             for (int index = 1; index < length - 1; index++)
  38.             {
  39.  
  40.                 if (arr[index] > arr[index - 1])
  41.                 {                    
  42.                     len++;
  43.                     arr[index] = arr[index - 1];
  44.                     if (arr[index] < arr[index - 1])
  45.                     {
  46.                         len++;
  47.                     }                    
  48.                 }
  49.                 else
  50.                 {                    
  51.                     len = 0;
  52.                 }
  53.                 if (len > bestLen)
  54.                 {          
  55.                     if (bestLen < len)
  56.                     {
  57.                         bestLen = len;                        
  58.                     }
  59.                     bestLen++;                    
  60.  
  61.                 }
  62.             }
  63.             Console.WriteLine("Max Sequence is: {0} ", bestLen);
  64.  
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement