Given an array arr[] consisting of N integers, the task is to find the length of the longest non-decreasing subsequence such that the difference between adjacent elements is at most 1.
Examples:
Input: arr[] = {8, 5, 4, 8, 4}
Output: 3
Explanation: {4, 4, 5}, {8, 8} are the two such non-decreasing subsequences of length 2 and 3 respectively. Therefore, the length of the longest of the two subsequences is 3.
Input: arr[] = {4, 13, 2, 3}
Output: 3
Explanation: {2, 3, 4}, {13} are the two such non-decreasing subsequences of length 3 and 1 respectively. Therefore, the length of the longest of the two subsequences is 3.
Approach: Follow the steps below to solve the problem:
- Sort the array arr[] in increasing order.
- Initialize a variable, say maxLen = 1, to store the maximum possible length of a subsequence. Initialize another variable, say len = 1 to store the current length for each subsequence.
- Traverse the array arr[] with a pointer i and for each element:
- Check if abs(arr[i] – arr[i – 1]) ? 1. If found to be true, then increment len by 1. Update maxLen = max(maxLen, len).
- Otherwise, set len = 1 i.e. start a new subsequence.
- Print the value of maxLen as the final answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void longestSequence( int arr[], int N)
{
if (N == 0) {
cout << 0;
return ;
}
sort(arr, arr+N);
int maxLen = 1;
int len = 1;
for ( int i = 1; i < N; i++) {
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
maxLen = max(maxLen, len);
}
else {
len = 1;
}
}
cout << maxLen;
}
int main()
{
int arr[] = { 8, 5, 4, 8, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
longestSequence(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void longestSequence( int arr[], int N)
{
if (N == 0 ) {
System.out.println( 0 );
return ;
}
Arrays.sort(arr);
int maxLen = 1 ;
int len = 1 ;
for ( int i = 1 ; i < N; i++) {
if (arr[i] == arr[i - 1 ]
|| arr[i] == arr[i - 1 ] + 1 ) {
len++;
maxLen = Math.max(maxLen, len);
}
else {
len = 1 ;
}
}
System.out.println(maxLen);
}
public static void main(String[] args)
{
int arr[] = { 8 , 5 , 4 , 8 , 4 };
int N = arr.length;
longestSequence(arr, N);
}
}
|
Python3
def longestSequence(arr, N):
if (N = = 0 ):
print ( 0 );
return ;
arr.sort();
maxLen = 1 ;
len = 1 ;
for i in range ( 1 ,N):
if (arr[i] = = arr[i - 1 ] or arr[i] = = arr[i - 1 ] + 1 ):
len + = 1 ;
maxLen = max (maxLen, len );
else :
len = 1 ;
print (maxLen);
if __name__ = = '__main__' :
arr = [ 8 , 5 , 4 , 8 , 4 ];
N = len (arr);
longestSequence(arr, N);
|
C#
using System;
public class GFG
{
static void longestSequence( int []arr, int N)
{
if (N == 0)
{
Console.WriteLine(0);
return ;
}
Array.Sort(arr);
int maxLen = 1;
int len = 1;
for ( int i = 1; i < N; i++) {
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
maxLen = Math.Max(maxLen, len);
}
else {
len = 1;
}
}
Console.WriteLine(maxLen);
}
public static void Main( string [] args)
{
int []arr = { 8, 5, 4, 8, 4 };
int N = arr.Length;
longestSequence(arr, N);
}
}
|
Javascript
<script>
function longestSequence(arr, N)
{
if (N == 0) {
document.write(0);
return ;
}
arr.sort();
var maxLen = 1;
var len = 1;
var i;
for (i = 1; i < N; i++) {
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
maxLen = Math.max(maxLen, len);
}
else {
len = 1;
}
}
document.write(maxLen);
}
var arr = [8, 5, 4, 8, 4];
var N = arr.length;
longestSequence(arr, N);
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1)