# 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 â <= (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 ` `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 = 1;` `Â Â Â Â ``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] = 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 ` `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 - arr;` `Â `Â  `Â Â Â Â ` `Â Â Â Â ` `Â Â Â Â ``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.