Danielos168

zad3

Jan 18th, 2021
679
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // C# program to find the  
  2. // length of the longest  
  3. // decreasing subsequence
  4. using System;
  5.  
  6. class GFG
  7. {
  8.  
  9.     // Function that returns the  
  10.     // length of the longest  
  11.     // decreasing subsequence
  12.     static int lds(int[] arr, int n)
  13.     {
  14.         int[] lds = new int[n];
  15.         int i, j, max = 0;
  16.  
  17.         // Initialize LDS with 1  
  18.         // for all index. The minimum  
  19.         // LDS starting with any
  20.         // element is always 1
  21.         for (i = 0; i < n; i++)
  22.             lds[i] = 1;
  23.  
  24.         // Compute LDS from every  
  25.         // index in bottom up manner
  26.         for (i = 1; i < n; i++)
  27.         for (j = 0; j < i; j++)
  28.             if (arr[i] <= arr[j] &&
  29.                 lds[i] < lds[j] + 1)
  30.                 lds[i] = lds[j] + 1;
  31.  
  32.         // Select the maximum  
  33.         // of all the LDS values
  34.         for (i = 0; i < n; i++)
  35.             if (max < lds[i])
  36.                 max = lds[i];
  37.  
  38.  
  39.  
  40.         foreach (var VARIABLE in lds)
  41.         {
  42.             Console.WriteLine(VARIABLE);
  43.         }
  44.         // returns the length
  45.         // of the LDS
  46.         return max;
  47.     }
  48.     // Driver Code
  49.     public static void Main()
  50.     {
  51.          int[] arr = {21,6,3,20,15,18,17,6,3,5,6,13,1,4,6,11};
  52.         //int[] arr = {50,3,10,7,40,80 ,7};
  53.         int n = arr.Length;
  54.         Console.Write("Length of LDS is " +
  55.                       lds(arr, n));
  56.         Console.ReadKey();
  57.     }
  58. }
RAW Paste Data