Dsa
Dynamic Programming
Easy
Longest Subsequence with Difference of 1
Given an array of integers, find the length of the longest subsequence such that the absolute difference between adjacent elements is 1. For example, if the input array is [1, 2, 3, 2, 3, 4, 5], the longest subsequence is [1, 2, 3, 2, 3, 4, 5] with a length of 7.
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 1000), the number of elements in the array.
- The second line contains n space-separated integers, the elements of the array.
Output Format:
- Output a single integer, the length of the longest subsequence.
Example:
Input:
7
1 2 3 2 3 4 5
Output:
7
Key concepts
dynamic_programmingsubsequencearray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now