gfg_200x200-min.png

Discover the Second Longest Increasing Subsequence

#contain

employing namespace std

  

int secondLis(int arr[], int n)

  

    

    

    

    vector<int> dpL(n, 1)

  

    

    

    

    

    vector<int> dpC(n, 1)

  

    for (int i = i < n i++)

        for (int j = 0 j < i j++)

            

            

            if (arr[i] <= arr[j])

                continue

  

            if (dpL[j] + 1> dpL[i])

                

                

                

                

                

                

                dpL[i] = dpL[j] + 1

                dpC[i] = dpC[j]

            

            else if (dpL[j] + 1 == dpL[i])

  

                

                

                

                dpC[i] += dpC[j]

        

    

  

    

    int maxLength =

    for (int i : dpL)

        maxLength = max(i, maxLength)

  

    

    int rely =

  

    

    for (int i = i < n i++)

  

        

        if (dpL[i] == maxLength)

            count += dpC[i]

    

  

    

    

    

    

    if (count> 1)

        return maxLength

    

    else

        return maxLength - 1

    

  

int principal()

    int arr[] = 1, -4, 3, 5, 9

    int n = sizeof(arr) / sizeof(arr[0])

    printf("Duration of second longest increasing "

           "subsequence is %dn",

           secondLis(arr, n))

  

    return


Supply backlink

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.