Length of longest non-decreasing subsequence such that difference between adjacent elements is at most one
Last Updated :
23 Jul, 2025
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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the longest non-decreasing
// subsequence with difference between
// adjacent elements exactly equal to 1
void longestSequence(int arr[], int N)
{
// Base case
if (N == 0) {
cout << 0;
return;
}
// Sort the array in ascending order
sort(arr, arr+N);
// Stores the maximum length
int maxLen = 1;
int len = 1;
// Traverse the array
for (int i = 1; i < N; i++) {
// If difference between current
// pair of adjacent elements is 1 or 0
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
// Extend the current sequence
// Update len and max_len
maxLen = max(maxLen, len);
}
else {
// Otherwise, start a new subsequence
len = 1;
}
}
// Print the maximum length
cout << maxLen;
}
// Driver Code
int main()
{
// Given array
int arr[] = { 8, 5, 4, 8, 4 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
// Function call to find the longest
// subsequence
longestSequence(arr, N);
return 0;
}
// This code is contributed by code_hunt.
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to find the longest non-decreasing
// subsequence with difference between
// adjacent elements exactly equal to 1
static void longestSequence(int arr[], int N)
{
// Base case
if (N == 0) {
System.out.println(0);
return;
}
// Sort the array in ascending order
Arrays.sort(arr);
// Stores the maximum length
int maxLen = 1;
int len = 1;
// Traverse the array
for (int i = 1; i < N; i++) {
// If difference between current
// pair of adjacent elements is 1 or 0
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
// Extend the current sequence
// Update len and max_len
maxLen = Math.max(maxLen, len);
}
else {
// Otherwise, start a new subsequence
len = 1;
}
}
// Print the maximum length
System.out.println(maxLen);
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 8, 5, 4, 8, 4 };
// Size of the array
int N = arr.length;
// Function call to find the longest
// subsequence
longestSequence(arr, N);
}
}
Python3
# Python program for the above approach
# Function to find the longest non-decreasing
# subsequence with difference between
# adjacent elements exactly equal to 1
def longestSequence(arr, N):
# Base case
if (N == 0):
print(0);
return;
# Sort the array in ascending order
arr.sort();
# Stores the maximum length
maxLen = 1;
len = 1;
# Traverse the array
for i in range(1,N):
# If difference between current
# pair of adjacent elements is 1 or 0
if (arr[i] == arr[i - 1] or arr[i] == arr[i - 1] + 1):
len += 1;
# Extend the current sequence
# Update len and max_len
maxLen = max(maxLen, len);
else:
# Otherwise, start a new subsequence
len = 1;
# Print the maximum length
print(maxLen);
# Driver Code
if __name__ == '__main__':
# Given array
arr = [8, 5, 4, 8, 4];
# Size of the array
N = len(arr);
# Function call to find the longest
# subsequence
longestSequence(arr, N);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the longest non-decreasing
// subsequence with difference between
// adjacent elements exactly equal to 1
static void longestSequence(int []arr, int N)
{
// Base case
if (N == 0)
{
Console.WriteLine(0);
return;
}
// Sort the array in ascending order
Array.Sort(arr);
// Stores the maximum length
int maxLen = 1;
int len = 1;
// Traverse the array
for (int i = 1; i < N; i++) {
// If difference between current
// pair of adjacent elements is 1 or 0
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
// Extend the current sequence
// Update len and max_len
maxLen = Math.Max(maxLen, len);
}
else {
// Otherwise, start a new subsequence
len = 1;
}
}
// Print the maximum length
Console.WriteLine(maxLen);
}
// Driver Code
public static void Main(string[] args)
{
// Given array
int []arr = { 8, 5, 4, 8, 4 };
// Size of the array
int N = arr.Length;
// Function call to find the longest
// subsequence
longestSequence(arr, N);
}
}
// This code is contributed by AnkThon
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to find the longest non-decreasing
// subsequence with difference between
// adjacent elements exactly equal to 1
function longestSequence(arr, N)
{
// Base case
if (N == 0) {
document.write(0);
return;
}
// Sort the array in ascending order
arr.sort();
// Stores the maximum length
var maxLen = 1;
var len = 1;
var i;
// Traverse the array
for (i = 1; i < N; i++) {
// If difference between current
// pair of adjacent elements is 1 or 0
if (arr[i] == arr[i - 1]
|| arr[i] == arr[i - 1] + 1) {
len++;
// Extend the current sequence
// Update len and max_len
maxLen = Math.max(maxLen, len);
}
else {
// Otherwise, start a new subsequence
len = 1;
}
}
// Print the maximum length
document.write(maxLen);
}
// Driver Code
// Given array
var arr = [8, 5, 4, 8, 4];
// Size of the array
var N = arr.length;
// Function call to find the longest
// subsequence
longestSequence(arr, N);
</script>
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Similar Reads
Longest non-decreasing subsequence having difference between adjacent elements less than D Given an array arr[] of N integers and an integer D, the task is to find the length of the longest non-decreasing subsequence such that the difference between every adjacent element is less than D. Examples: Input: arr[] = {1, 3, 2, 4, 5}, D = 2Output: 3Explanation:Consider the subsequence as {3, 4,
6 min read
Longest subsequence such that difference between adjacent elements is K Given an array arr[] of size N and an integer K, the task is to find the longest subsequence such that the difference between adjacents is K. Example: Input: arr[]={1, 2, 3, 4, 5, 3, 2}, K=1Output: 6Explanation: The longest subsequence with the difference between the adjacent elements as 1 is: {1, 2
5 min read
Print longest Subsequence such that difference between adjacent elements is K Given an array arr[] of size N, and integer K. The task is to find the longest subsequence with the difference between adjacent elements as K Examples: Input: arr[] = { 5, 5, 5, 10, 8, 6, 12, 13 }, K = 1Output: {5, 6} Input: arr[] = {4, 6, 7, 8, 9, 8, 12, 14, 17, 15}, K = 2Output: {4, 6, 8} Approach
9 min read
Length of Longest subarray such that difference between adjacent elements is K Given an array arr[] of size N, and integer K. The task is to find the length of the longest subarray with the difference between adjacent elements as K. Examples: Input: arr[] = { 5, 5, 5, 10, 8, 6, 12, 13 }, K =1Output: 2Explanation: Only one subarray which have difference between adjacents as 1 i
5 min read
Longest Subsequence such that difference between adjacent elements is either A or B Given an array arr of size N, and two integers A and B. The task is to find the length of the longest subsequence with the difference between adjacent elements as either A or B. Example: Input : arr[]={ 5, 5, 5, 10, 8, 6, 12, 13 }, A=0, B=1Output : 4Explanation : Maximum length subsequence is {5,5,5
6 min read
Longest subsequence such that difference between adjacents is one Given an array arr[] of size n, the task is to find the longest subsequence such that the absolute difference between adjacent elements is 1.Examples: Input: arr[] = [10, 9, 4, 5, 4, 8, 6]Output: 3Explanation: The three possible subsequences of length 3 are [10, 9, 8], [4, 5, 4], and [4, 5, 6], wher
15+ min read