Longest non-decreasing Subsequence with adjoining variations

Given an array arr[] of N components, the duty is to search out the period of the longest non-decreasing subsequence such that the diversities between adjoining components are non-decreasing. For indices i, j, and okay: 
i < j < okay, ai − aj ≤ aj − ak

Examples:

Enter: N = 9, arr[] = [1, 3, 5, 4, 7, 8, 10, 6, 9]
Output: 6
Clarification: Longest non-decreasing Subsequence is [1, 3, 5, 7, 8, 10]. Right here, the subsequence satisfies the given situation, (3 – 1) <= (5 – 3), (4 – 3) <= (5 – 4), (7 – 5) <= (8 – 7), (8 – 7) <= (10 – 8), and (10 – 8) <= (9 – 6). The period of such Longest Subsequence on this case is 6.

Enter: N = 8, arr[] = [1, 4, 5, 6, 2, 3, 8, 9]
Output: 5
Clarification: Longest non-decreasing Subsequence with given situation is [1, 4, 6, 8, 9].

Method: To resolve the issue practice the beneath concept: 

The theory is to make use of a dp array to retailer the period of such longest subsequence as much as index i of the given array. Then, we traverse the array for every index i, and for every index j (0 ≤ j < i), we take a look at if the subsequence [j, i] bureaucracy a subsequence. If it does, we replace the price of dp[i] with the utmost price of dp[j]+1. After all, we go back the utmost price within the dp array.

Underneath are the stairs for the above manner:

  • Initialize a variable say n to retailer the dimensions of the enter array arr.
  • Test if the dimensions of the given array is lower than or equivalent to two, and go back the dimensions of the array.
  • Initialize an array dp of measurement n  all 0’s.
  • Initialize the primary two values of the dp to at least one and a couple of respectively.
  • Initialize a variable say ans = 2.
  • Iterate from index i = 2 to i < n,
    • Throughout the for loop, run some other loop from index j = 1 to j < i
      • Test if the present component and the former component shape a subsequence with the given situation, if (arr[i] – arr[j] == arr[j] – arr[j-1]),
      • Replace the dp[] array with the utmost price between the present dp price of i and dp price of j + 1.
    • Replace the ans variable with the utmost price between itself and the present dp price of i.
  • Go back ans.

Underneath is the implementation for the above manner:

C++

#come with <bits/stdc++.h>

the usage of namespace std;

  

int longest_subsequence(vector<int> arr)

{

  

    int n = arr.measurement();

  

    

    

    if (n <= 2)

        go back n;

  

    

    int dp[n];

    memset(dp, 0, sizeof(dp));

  

    

    

    dp[0] = 1;

    dp[1] = 2;

  

    

    int ans = 2;

  

    

    for (int i = 2; i < n; i++) {

  

        

        for (int j = 1; j < i; j++) {

  

            

            

            

            if (arr[i] - arr[j] == arr[j] - arr[j - 1])

  

                

                dp[i] = max(dp[i], dp[j] + 1);

        }

  

        

        ans = max(ans, dp[i]);

    }

  

    

    go back ans;

}

  

int primary()

{

    vector<int> arr = { 1, 2, 3, 5, 6, 8 };

  

    

    cout << "Longest Subsequence: "

         << longest_subsequence(arr) << endl;

    go back 0;

}

Java

import java.util.Arrays;

  

public magnificence LongestConvexSubsequence {

    public static int longestConvexSubsequence(int[] arr)

    {

        int n = arr.period;

        

        if (n <= 2)

            go back n;

  

        

        int[] dp = new int[n];

        Arrays.fill(dp, 2);

  

        

        int ans = 2;

        for (int i = 2; i < n; i++) {

            for (int j = 1; j < i; j++) {

                

                if (arr[i] - arr[j] == arr[j] - arr[j - 1])

                    dp[i] = Math.max(dp[i], dp[j] + 1);

            }

            ans = Math.max(ans, dp[i]);

        }

        go back ans;

    }

  

    

    public static void primary(String[] args)

    {

        int[] arr = { 1, 2, 3, 5, 6, 8 };

        Machine.out.println("Longest Convex Subsequence: " + longestConvexSubsequence(arr));

    }

}

Python3

  

def longest_convex_subsequence(arr):

    n = len(arr)

   

    if n <= 2:

        go back n

     

    dp = [2] * n

  

    

    ans = 2

    

    for i in vary(2, n):

      

        for j in vary(1, i):

          

            if arr[i] - arr[j] == arr[j] - arr[j-1]:

              

                dp[i] = max(dp[i], dp[j] + 1)

        

        ans = max(ans, dp[i])

  

  

    go back ans

  

arr = [1, 2, 3, 5, 6, 8]

print("Longest Convex Subsequence: ", longest_convex_subsequence(arr))

Output
Longest Subsequence: 3

Time Complexity: O(n2)
Auxiliary House: O(n), the place n is the period of the given array.

Grasping Method: To resolve the issue practice the beneath concept:

The theory is to make use of a variable ‘len’ to retailer the period of the longest subsequence observed up to now, and some other variable ‘curr_len’ to retailer the period of the present subsequence. Additionally, use a variable ‘diff’ to retailer the adaptation between two consecutive components of the present subsequence. Initialize ‘len’ and ‘curr_len’ with 2, as any two components shape a subsequence. Traverse the array and take a look at if the adaptation between two consecutive components is equal to the ‘diff’ variable, increment ‘curr_len’, else, replace the ‘len’ variable with the utmost price between ‘len’ and ‘curr_len’, and reset ‘curr_len’ to 2, and ‘diff’ to the adaptation between the 2 consecutive components. Go back the utmost price between ‘len’ and ‘curr_len’.

Underneath are the stairs for the above manner:

  • Initialize a variable say n to retailer the dimensions of the enter array arr.
  • Test if the dimensions of the given array is lower than or equivalent to two, and go back the dimensions of the array.
  • Initialize a variable len = 2, because the period of the smallest subsequence, is two.
  • Initialize a variable diff to the adaptation between the second one and the primary components of the enter array arr.
  • Initialize the present period of subsequence as 2, curr_len = 2.
  • Traverse the array from index i = 2 to i < n and take a look at if the adaptation between the present component and the former component is equal to the adaptation between the former two components, increment curr_length via 1.
  • Else if the adaptation between the present component and the former component isn’t the similar as the adaptation between the former two components, replace the adaptation between the consecutive components, diff = arr[i] – arr[i-1].
  • Replace period of longest subsequence, len = max(len, curr_len).
  • The present period of the subsequence is reset to 2.
  • As soon as the for loop completes, the period of the longest subsequence is up to date via taking the utmost price between itself and the present period of the subsequence, len = max(len, curr_len).
  • Go back the price of variable len.

Underneath is the implementation for the above manner:

C++

#come with <bits/stdc++.h>

the usage of namespace std;

  

int longest_subsequence(vector<int> arr)

{

    int n = arr.measurement();

  

    

    

    

    

    if (n <= 2)

        go back n;

  

    

    

    int len = 2;

  

    

    

    int diff = arr[1] - arr[0];

  

    

    

    int curr_len = 2;

  

    

    for (int i = 2; i < n; i++) {

  

        

        

        

        

        

        if (arr[i] - arr[i - 1] == diff) {

  

            

            

            curr_len++;

        }

        else {

  

            

            

            

            diff = arr[i] - arr[i - 1];

  

            

            

            len = max(len, curr_len);

  

            

            

            

            curr_len = 2;

        }

    }

  

    

    

    len = max(len, curr_len);

  

    

    

    go back len;

}

  

int primary()

{

    vector<int> arr = { 1, 2, 3, 5, 6, 8 };

  

    

    cout << "Longest non-decreasing Subsequence: "

         << longest_subsequence(arr) << endl;

    go back 0;

}

Java

  

import java.util.Arrays;

  

public magnificence LongestConvexSubsequence {

    public static int longestConvexSubsequence(int[] arr)

    {

        int n = arr.period;

  

        

        if (n <= 2)

            go back n;

  

        

        int len = 2;

        int diff = arr[1] - arr[0];

        int curr_len = 2;

  

        

        for (int i = 2; i < n; i++) {

  

            

            if (arr[i] - arr[i - 1] == diff) {

                curr_len++;

            }

            else {

                

                diff = arr[i] - arr[i - 1];

                len = Math.max(len, curr_len);

                curr_len = 2;

            }

        }

  

        len = Math.max(len, curr_len);

        go back len;

    }

  

    

    public static void primary(String[] args)

    {

        int[] arr = { 1, 2, 3, 5, 6, 8 };

        Machine.out.println("Longest Convex Subsequence: " + longestConvexSubsequence(arr));

    }

}

Python3

  

def longest_convex_subsequence(arr):

    n = len(arr)

    

    if n <= 2:    

        go back n

  

    

    max_len = 2   

    

    diff = arr[1] - arr[0]   

    

    curr_len = 2  

    

    for i in vary(2, n):  

      

        if arr[i] - arr[i-1] == diff: 

          

            curr_len += 1   

        

        else:  

          

            diff = arr[i] - arr[i-1]   

            

            max_len = max(max_len, curr_len)  

            

            curr_len = 2   

     

    max_len = max(max_len, curr_len)

    

    go back max_len   

  

arr = [1, 2, 3, 5, 6, 8]   

print("Longest Convex Subsequence: ", longest_convex_subsequence(arr))   

Output
Longest non-decreasing Subsequence: 3

Time Complexity: O(n), the place n is the period of the given array.
Auxiliary House: O(1), since we aren’t the usage of any further knowledge constructions.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: