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++
#include <bits/stdc++.h>
using namespace std;
int maxLenSub(int arr[], int n)
{
int mls[n], max = 0;
for (int i=0; i<n; i++)
mls[i] = 1;
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;
for (int i=0; i<n; i++)
if (max < mls[i])
max = mls[i];
return max;
}
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
import java.util.*;
class GFG {
public static int maxLenSub(int arr[], int n)
{
int mls[] = new int[n], max = 0;
for (int i = 0; i < n; i++)
mls[i] = 1;
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;
for (int i = 0; i < n; i++)
if (max < mls[i])
max = mls[i];
return max;
}
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));
}
}
Python3
def maxLenSub( arr, n):
mls=[]
max = 0
for i in range(n):
mls.append(1)
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
for i in range(n):
if (max < mls[i]):
max = mls[i]
return max
arr = [2, 5, 6, 3, 7, 6, 5, 8]
n = len(arr)
print("Maximum length subsequence = ",maxLenSub(arr, n))
C#
using System;
class GFG {
public static int maxLenSub(int[] arr, int n)
{
int[] mls = new int[n];
int max = 0;
for (int i = 0; i < n; i++)
mls[i] = 1;
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;
for (int i = 0; i < n; i++)
if (max < mls[i])
max = mls[i];
return max;
}
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));
}
}
PHP
<?php
function maxLenSub($arr, $n)
{
$mls = array(); $max = 0;
for($i = 0; $i < $n; $i++)
$mls[$i] = 1;
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;
for($i = 0; $i < $n; $i++)
if ($max < $mls[$i])
$max = $mls[$i];
return $max;
}
$arr = array(2, 5, 6, 3, 7, 6, 5, 8);
$n = count($arr);
echo "Maximum length subsequence = "
, maxLenSub($arr, $n);
?>
Javascript
<script>
function maxLenSub(arr, n)
{
let mls = new Array(n).fill(1), max = 0;
for (let i = 0; i < n; i++)
mls[i] = 1;
for (let i = 1; i < n; i++)
for (let j = 0; j < i; j++)
if (Math.abs(arr[i] - arr[j]) <= 1
&& mls[i] < mls[j] + 1)
mls[i] = mls[j] + 1;
for (let i = 0; i < n; i++)
if (max < mls[i])
max = mls[i];
return max;
}
let arr = [2, 5, 6, 3, 7, 6, 5, 8];
let n = arr.length;
document.write("Maximum length subsequence = "+
maxLenSub(arr, n));
</script>
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 If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
30 Apr, 2021
Like Article
Save Article