Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C# program to find the
- // length of the longest
- // decreasing subsequence
- using System;
- class GFG
- {
- // Function that returns the
- // length of the longest
- // decreasing subsequence
- static int lds(int[] arr, int n)
- {
- int[] lds = new int[n];
- int i, j, max = 0;
- // Initialize LDS with 1
- // for all index. The minimum
- // LDS starting with any
- // element is always 1
- for (i = 0; i < n; i++)
- lds[i] = 1;
- // Compute LDS from every
- // index in bottom up manner
- for (i = 1; i < n; i++)
- for (j = 0; j < i; j++)
- if (arr[i] <= arr[j] &&
- lds[i] < lds[j] + 1)
- lds[i] = lds[j] + 1;
- // Select the maximum
- // of all the LDS values
- for (i = 0; i < n; i++)
- if (max < lds[i])
- max = lds[i];
- foreach (var VARIABLE in lds)
- {
- Console.WriteLine(VARIABLE);
- }
- // returns the length
- // of the LDS
- return max;
- }
- // Driver Code
- public static void Main()
- {
- int[] arr = {21,6,3,20,15,18,17,6,3,5,6,13,1,4,6,11};
- //int[] arr = {50,3,10,7,40,80 ,7};
- int n = arr.Length;
- Console.Write("Length of LDS is " +
- lds(arr, n));
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement