Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MaxIncreasingSequence
- {
- class MaxIncreasingSequence
- {
- public static void Main(string[] args)
- {
- //Console.WriteLine("No of elements in array: ");
- //int n = int.Parse(Console.ReadLine());
- //arr[]
- int n = 10;
- int[] arr = new int[n];
- //read from the console each element of the array
- for (int i = 0; i < n; i++)
- {
- Console.Write("Array[{0}] = ", i);
- arr[i] = int.Parse(Console.ReadLine());
- }
- //len[]
- int[] len = new int[n];
- for(int i = 0; i < n; i++)
- {
- len[i] = 1;
- }
- //correct len[]
- for(int indexCount = 0; indexCount < n; indexCount++)
- {
- for(int indexNumber = 0; indexNumber < indexCount; indexNumber++)
- {
- if(arr[indexCount] > arr[indexNumber] && len[indexCount] < len[indexCount] + 1)
- {
- len[indexCount] = len[indexNumber] + 1;
- }
- }
- }
- //print the new len[]
- //just to keep track on numebers
- Console.WriteLine();
- Console.Write("{");
- for(int i = 0; i < n; i++)
- {
- Console.Write(" " + len[i] + " ");
- }
- Console.WriteLine("}");
- //search for the max number in len[]
- int max = int.MinValue;
- int maxPosition = 0;
- for(int i = 0; i < len.Length; i++)
- {
- if(len[i] > max)
- {
- max = len[i];
- maxPosition = i;
- }
- }
- //create the result in result[]
- int[] result = new int[max];
- int j = (max - 1);
- int maxCount = max;
- for(int k = maxPosition; k > 0; k--)
- {
- if(len[k] == max)
- {
- result[j] = arr[k];
- max--;
- j--;
- }
- }
- //preint the result[]
- Console.WriteLine();
- Console.Write("{");
- for(int i = 0; i < maxCount; i++)
- {
- Console.Write(" " + result[i] + " ");
- }
- Console.WriteLine("}");
- /*read the values of 'n' number of elements
- * from user's input via the console
- */
- //for (int i = 0; i < length; i++)
- //{
- // Console.Write("Enter {0} element: ", i);
- // arr[i] = int.Parse(Console.ReadLine());
- //}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement