Find a Strictly Increasing Subsequence with Limited Numeric Steps

Quick Overview

Find the maximum length of a strictly increasing subsequence whose adjacent selected values differ by at most k.

Find a Strictly Increasing Subsequence with Limited Numeric Steps

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

### Problem Choose a subsequence of `nums` that is strictly increasing and whose neighboring retained values differ by at most k. Return its maximum length, retaining original index order while permitting skipped positions. ```cpp class Solution { public: int lengthOfLIS(vector<int>& nums, int k); }; ``` ### Examples **Example 1:** ```text Input: nums = [1,3,2,4], k = 2 Output: 3 Explanation: Both [1,3,4] and [1,2,4] work. All four values are not increasing. ``` **Example 2:** ```text Input: nums = [2,2,3], k = 1 Output: 2 Explanation: Equal 2 values cannot extend a strictly increasing subsequence, but either occurrence can precede 3. ``` ### Constraints - `1 <= nums.length <= 100000`. - `1 <= nums[i], k <= 100000`. ### Clarifying Questions - May equal values appear consecutively in the selected subsequence? No; increase is strict. - Can positions be skipped? Yes, while keeping their original order. - Is a gap equal to `k` allowed? Yes. - Is the bound on adjacent selected values or all pairs? Adjacent selected values. ```hint Check the boundary Check repeated values and a numeric gap exactly equal to `k`. ```

Overview: Find the maximum length of a strictly increasing subsequence whose adjacent selected values differ by at most k.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Choose a subsequence of nums that is strictly increasing and whose neighboring retained values differ by at most k. Return its maximum length, retaining original index order while permitting skipped positions.

class Solution {
public:
    int lengthOfLIS(vector<int>& nums, int k);
};

Examples

Example 1:

Input: nums = [1,3,2,4], k = 2
Output: 3
Explanation: Both [1,3,4] and [1,2,4] work. All four values are not increasing.

Example 2:

Input: nums = [2,2,3], k = 1
Output: 2
Explanation: Equal 2 values cannot extend a strictly increasing subsequence, but either occurrence can precede 3.

Constraints

  • 1 <= nums.length <= 100000 .
  • 1 <= nums[i], k <= 100000 .

Clarifying Questions Guidance

  • May equal values appear consecutively in the selected subsequence? No; increase is strict.
  • Can positions be skipped? Yes, while keeping their original order.
  • Is a gap equal to k allowed? Yes.
  • Is the bound on adjacent selected values or all pairs? Adjacent selected values.