svetlozar_kirkov

Maximal sequence of consecutively placed increasing integers

Sep 30th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleTesting
  6. {
  7.     internal class ConsoleTesting
  8.     {
  9.         private static void Main()
  10.         {
  11.             Console.Write("Enter the length of the array: ");
  12.             int n = int.Parse(Console.ReadLine());
  13.             var arrayInt = new int[n];
  14.             var seqList = new List<string>();
  15.             var seqCounts = new List<int>();
  16.  
  17.             for (int i = 0; i < arrayInt.Length; i++)
  18.             {
  19.                 Console.Write("Index \"{0}\": ", i);
  20.                 arrayInt[i] = int.Parse(Console.ReadLine());
  21.             }
  22.  
  23.             for (int i = 0; i < arrayInt.Length;)
  24.             {
  25.                 int temp = arrayInt[i];
  26.                 var tempSeq = new List<string>();
  27.                 tempSeq.Add(Convert.ToString(temp));
  28.                 int x = 1;
  29.                 while (i + 1 < arrayInt.Length && arrayInt[i + 1] == temp+x)
  30.                 {
  31.                     tempSeq.Add(Convert.ToString(arrayInt[i + 1]));
  32.                     i++;
  33.                     x++;
  34.                 }
  35.                 if (tempSeq.Count > 1)
  36.                 {
  37.                     string tempovSeq = string.Join(",", tempSeq);
  38.                     seqList.Add(tempovSeq);
  39.                     seqCounts.Add(tempSeq.Count);
  40.                     i++;
  41.                 }
  42.                 else
  43.                 {
  44.                     i++;
  45.                 }
  46.             }
  47.  
  48.             if (seqList.Count == 0)
  49.             {
  50.                 Console.WriteLine();
  51.                 Console.WriteLine("No sequences with increasing members found! Exiting...");
  52.                 Console.WriteLine();
  53.                 return;
  54.             }
  55.             else if (seqList.Count == 1)
  56.             {
  57.                 Console.WriteLine();
  58.                 Console.WriteLine("Only one sequence with increasing members found: " + seqList[0]);
  59.                 Console.WriteLine();
  60.                 return;
  61.             }
  62.             Console.WriteLine();
  63.             Console.WriteLine("Sequences with increasing members: ");
  64.             foreach (string seq in seqList)
  65.             {
  66.                 Console.WriteLine(seq);
  67.             }
  68.             Console.WriteLine();
  69.  
  70.             var allAreSame = seqCounts.Distinct().Count() == 1;
  71.            
  72.             if (allAreSame==true)
  73.             {
  74.                 Console.WriteLine("All sequences have equal length");
  75.                 Console.WriteLine();
  76.                 return;
  77.             }
  78.             else
  79.             {
  80.                 int maxIndex = seqCounts.IndexOf(seqCounts.Max());
  81.                 Console.WriteLine("The longest sequence is: \"{0}\"", seqList[maxIndex]);
  82.                 Console.WriteLine();
  83.                 return;
  84.             }
  85.            
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment