You are given an integer array nums and an integer k. Your task is to find the longest subsequence that satisfies two important conditions:

  1. The subsequence is strictly increasing
  2. The difference between any two adjacent elements in the subsequence is at most k

A subsequence is derived from the original array by deleting some or no elements without changing the order of the remaining elements. For example, from array [4,2,1,4,3,4,5,8,15], a valid subsequence could be [2,4,5,8].

Return the length of the longest subsequence that meets both requirements.

Example: If nums = [4,2,1,4,3,4,5,8,15] and k = 3, one possible longest increasing subsequence with difference ≤ 3 could be [1,3,4,5,8] with length 5.

Input & Output

example_1.py — Basic Case
$ Input: nums = [4,2,1,4,3,4,5,8,15], k = 3
Output: 5
💡 Note: One possible longest increasing subsequence is [1,3,4,5,8] with length 5. Each adjacent pair has difference ≤ 3: 3-1=2, 4-3=1, 5-4=1, 8-5=3.
example_2.py — Small Difference
$ Input: nums = [7,4,5,1,8,12,4,7], k = 5
Output: 4
💡 Note: One possible longest increasing subsequence is [4,5,7,12] with length 4. Differences: 5-4=1, 7-5=2, 12-7=5 (all ≤ 5).
example_3.py — Edge Case
$ Input: nums = [1,5], k = 1
Output: 1
💡 Note: Cannot form increasing subsequence of length > 1 because 5-1=4 > k=1. Maximum length is 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i], k ≤ 105
  • The array can contain duplicate values
  • Subsequence must be strictly increasing (no equal elements)

Visualization

Tap to expand
Longest Increasing Subsequence II INPUT Array nums: 4 2 1 4 3 4 5 8 15 indices: 0 1 2 3 4 5 6 7 8 k = 3 Constraints: 1. Strictly increasing 2. Adjacent diff <= k Valid transitions: 1 --> 2,3,4 (diff <= 3) 3 --> 4,5,6 (diff <= 3) 5 --> 15 INVALID ALGORITHM STEPS 1 Segment Tree Setup Range max query for dp[v-k..v-1] 2 Process Each Element For each num, query best prev 3 Update DP Value dp[num] = max_in_range + 1 4 Track Maximum Answer = max of all dp values DP Table Updates: num=4: dp[4]=1 num=2: dp[2]=1 num=1: dp[1]=1 num=4: dp[4]=2 (1+1) num=3: dp[3]=2 (1+1) num=4: dp[4]=3 (2+1) num=5: dp[5]=4 (3+1) num=8: dp[8]=5 (4+1) num=15: dp[15]=1 (no valid) FINAL RESULT Longest Valid Subsequence: 1 3 4 5 8 Differences between adjacent: 3-1=2, 4-3=1, 5-4=1, 8-5=3 All diffs <= k=3 [OK] OUTPUT 5 Length of subsequence [1, 3, 4, 5, 8] [OK] Verified! Key Insight: Use Segment Tree for O(log n) range maximum queries. For each element v, we need the longest subsequence ending at any value in [v-k, v-1]. Segment tree enables efficient range queries and point updates, achieving O(n log M) time complexity where M is the maximum value in nums. TutorialsPoint - Longest Increasing Subsequence II | Optimal Solution (Segment Tree)
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen