Advertisement
yanabg

MaxIncreasingSequence

Nov 28th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MaxIncreasingSequence
  4. {
  5.     class MaxIncreasingSequence
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             //Console.WriteLine("No of elements in array: ");
  10.             //int n = int.Parse(Console.ReadLine());
  11.  
  12.             //arr[]
  13.             int n = 10;
  14.             int[] arr = new int[n];
  15.  
  16.             //read from the console each element of the array
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 Console.Write("Array[{0}] = ", i);
  20.                 arr[i] = int.Parse(Console.ReadLine());
  21.  
  22.             }
  23.  
  24.             //len[]
  25.             int[] len = new int[n];
  26.             for(int i = 0; i < n; i++)
  27.             {
  28.                 len[i] = 1;
  29.             }
  30.  
  31.             //correct len[]
  32.             for(int indexCount = 0; indexCount < n; indexCount++)
  33.             {
  34.                 for(int indexNumber = 0; indexNumber < indexCount; indexNumber++)
  35.                 {
  36.                     if(arr[indexCount] > arr[indexNumber] && len[indexCount] < len[indexCount] + 1)
  37.                     {
  38.                         len[indexCount] = len[indexNumber] + 1;
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             //print the new len[]
  44.             //just to keep track on numebers
  45.             Console.WriteLine();
  46.             Console.Write("{");
  47.             for(int i = 0; i < n; i++)
  48.             {
  49.                 Console.Write(" " + len[i] + " ");
  50.             }
  51.             Console.WriteLine("}");
  52.  
  53.             //search for the max number in len[]
  54.             int max = int.MinValue;
  55.             int maxPosition = 0;
  56.             for(int i = 0; i < len.Length; i++)
  57.             {
  58.                 if(len[i] > max)
  59.                 {
  60.                     max = len[i];
  61.                     maxPosition = i;
  62.                 }
  63.             }
  64.  
  65.             //create the result in result[]
  66.             int[] result = new int[max];
  67.             int j = (max - 1);
  68.             int maxCount = max;
  69.             for(int k = maxPosition; k > 0; k--)
  70.             {
  71.                 if(len[k] == max)
  72.                 {
  73.                     result[j] = arr[k];
  74.                     max--;
  75.                     j--;
  76.                 }
  77.             }
  78.             //preint the result[]
  79.             Console.WriteLine();
  80.             Console.Write("{");
  81.             for(int i = 0; i < maxCount; i++)
  82.             {
  83.                 Console.Write(" " + result[i] + " ");
  84.             }
  85.             Console.WriteLine("}");
  86.  
  87.  
  88.             /*read the values of 'n' number of elements
  89.              * from user's input via the console
  90.              */
  91.             //for (int i = 0; i < length; i++)
  92.  
  93.             //{
  94.             //    Console.Write("Enter {0} element: ", i);
  95.             //    arr[i] = int.Parse(Console.ReadLine());
  96.             //}
  97.  
  98.            
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement