Given an array of n integers. The problem is to find maximum length of the subsequence with difference between adjacent elements as either 0 or 1.
Examples:
Input : arr[] = {2, 5, 6, 3, 7, 6, 5, 8}
Output : 5
The subsequence is {5, 6, 7, 6, 5}.
Input : arr[] = {-2, -1, 5, -1, 4, 0, 3}
Output : 4
The subsequence is {-2, -1, -1, 0}.
Source: Expedia Interview Experience | Set 12
The solution to this problem closely resembles the Longest Increasing Subsequence problem. The only difference is that here we have to check whether the absolute difference between the adjacent elements of the subsequence is either 0 or 1.
C++
// C++ implementation to find maximum length // subsequence with difference between adjacent // elements as either 0 or 1 #include <bits/stdc++.h> using namespace std; // function to find maximum length subsequence // with difference between adjacent elements as // either 0 or 1 int maxLenSub(int arr[], int n) { int mls[n], max = 0; // Initialize mls[] values for all indexes for (int i=0; i<n; i++) mls[i] = 1; // Compute optimized maximum length subsequence // values in bottom up manner for (int i=1; i<n; i++) for (int j=0; j<i; j++) if (abs(arr[i] - arr[j]) <= 1 && mls[i] < mls[j] + 1) mls[i] = mls[j] + 1; // Store maximum of all 'mls' values in 'max' for (int i=0; i<n; i++) if (max < mls[i]) max = mls[i]; // required maximum length subsequence return max; } // Driver program to test above int main() { int arr[] = {2, 5, 6, 3, 7, 6, 5, 8}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Maximum length subsequence = " << maxLenSub(arr, n); return 0; } |
Java
// JAVA Code for Maximum length subsequence // with difference between adjacent elements // as either 0 or 1 import java.util.*; class GFG { // function to find maximum length subsequence // with difference between adjacent elements as // either 0 or 1 public static int maxLenSub(int arr[], int n) { int mls[] = new int[n], max = 0; // Initialize mls[] values for all indexes for (int i = 0; i < n; i++) mls[i] = 1; // Compute optimized maximum length // subsequence values in bottom up manner for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) if (Math.abs(arr[i] - arr[j]) <= 1 && mls[i] < mls[j] + 1) mls[i] = mls[j] + 1; // Store maximum of all 'mls' values in 'max' for (int i = 0; i < n; i++) if (max < mls[i]) max = mls[i]; // required maximum length subsequence return max; } /* Driver program to test above function */ public static void main(String[] args) { int arr[] = {2, 5, 6, 3, 7, 6, 5, 8}; int n = arr.length; System.out.println("Maximum length subsequence = "+ maxLenSub(arr, n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python implementation to find maximum length # subsequence with difference between adjacent # elements as either 0 or 1 # function to find maximum length subsequence # with difference between adjacent elements as # either 0 or 1 def maxLenSub( arr, n): mls=[] max = 0 #Initialize mls[] values for all indexes for i in range(n): mls.append(1) #Compute optimized maximum length subsequence # values in bottom up manner for i in range(n): for j in range(i): if (abs(arr[i] - arr[j]) <= 1 and mls[i] < mls[j] + 1): mls[i] = mls[j] + 1 # Store maximum of all 'mls' values in 'max' for i in range(n): if (max < mls[i]): max = mls[i] #required maximum length subsequence return max #Driver program to test above arr = [2, 5, 6, 3, 7, 6, 5, 8] n = len(arr) print("Maximum length subsequence = ",maxLenSub(arr, n)) #This code is contributed by "Abhishek Sharma 44" |
C#
// C# Code for Maximum length subsequence // with difference between adjacent elements // as either 0 or 1 using System; class GFG { // function to find maximum length subsequence // with difference between adjacent elements as // either 0 or 1 public static int maxLenSub(int[] arr, int n) { int[] mls = new int[n]; int max = 0; // Initialize mls[] values for all indexes for (int i = 0; i < n; i++) mls[i] = 1; // Compute optimized maximum length // subsequence values in bottom up manner for (int i = 1; i < n; i++) for (int j = 0; j < i; j++) if (Math.Abs(arr[i] - arr[j]) <= 1 && mls[i] < mls[j] + 1) mls[i] = mls[j] + 1; // Store maximum of all 'mls' values in 'max' for (int i = 0; i < n; i++) if (max < mls[i]) max = mls[i]; // required maximum length subsequence return max; } /* Driver program to test above function */ public static void Main() { int[] arr = { 2, 5, 6, 3, 7, 6, 5, 8 }; int n = arr.Length; Console.Write("Maximum length subsequence = " + maxLenSub(arr, n)); } } // This code is contributed by Sam007 |
PHP
<?php // PHP implementation to find maximum length // subsequence with difference between adjacent // elements as either 0 or 1 // function to find maximum length subsequence // with difference between adjacent elements as // either 0 or 1 function maxLenSub($arr, $n) { $mls = array(); $max = 0; // Initialize mls[] values // for all indexes for($i = 0; $i < $n; $i++) $mls[$i] = 1; // Compute optimized maximum // length subsequence // values in bottom up manner for ($i = 1; $i < $n; $i++) for ( $j = 0; $j < $i; $j++) if (abs($arr[$i] - $arr[$j]) <= 1 and $mls[$i] < $mls[$j] + 1) $mls[$i] = $mls[$j] + 1; // Store maximum of all // 'mls' values in 'max' for($i = 0; $i < $n; $i++) if ($max < $mls[$i]) $max = $mls[$i]; // required maximum // length subsequence return $max; } // Driver Code $arr = array(2, 5, 6, 3, 7, 6, 5, 8); $n = count($arr); echo "Maximum length subsequence = " , maxLenSub($arr, $n); // This code is contributed by anuj_67. ?> |
Output:
Maximum length subsequence = 5
Time Complexity: O(n2)
Auxiliary Space: O(n)
Maximum length subsequence with difference between adjacent elements as either 0 or 1 | Set 2
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Maximum subsequence sum of at most K-distant adjacent elements
- Maximum value with the choice of either dividing or considering as it is
- Remove minimum elements from either side such that 2*min becomes more than max
- Longest subsequence such that adjacent elements have at least one common digit
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Maximum Sum Subsequence of length k
- Longest subsequence with different adjacent characters
- Maximum sum such that no two elements are adjacent
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Maximum sum such that exactly half of the elements are selected and no two adjacent
Improved By : vt_m

