Find the longest subsequence with a fixed difference between adjacent elements

Quick Overview

Given an integer array and a fixed difference, find the length of the longest subsequence in which each element minus the one before it equals that difference. Tests dynamic programming keyed by value, hash map lookups, and a linear-time approach that holds up for arrays of up to 100,000 elements.

Find the longest subsequence with a fixed difference between adjacent elements

Company: GitHub

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `arr` and an integer `difference`, return the length of the longest subsequence of `arr` that is an arithmetic sequence in which the difference between adjacent elements equals `difference`. A subsequence is a sequence that can be derived from `arr` by deleting some or no elements without changing the order of the remaining elements. ### Function Signature ```python def longest_subsequence_with_difference(arr: list[int], difference: int) -> int: ``` ### Rules - "Adjacent" means consecutive in the subsequence: for every pair of neighbors, the later element minus the earlier one equals `difference`. - `difference` may be negative or zero. With a difference of zero, the subsequence consists of equal values. - A single element is a valid subsequence of length 1, so the answer is always at least 1. ### Constraints - `1 <= len(arr) <= 10^5` - `-10^4 <= arr[i], difference <= 10^4` ### Examples ```text Input: arr = [1, 2, 3, 4], difference = 1 Output: 4 ``` The whole array is the subsequence `[1, 2, 3, 4]`, and each step adds 1. ```text Input: arr = [1, 3, 5, 7], difference = 1 Output: 1 ``` No two elements differ by exactly 1, so the best is any single element. ```text Input: arr = [1, 5, 7, 8, 5, 3, 4, 2, 1], difference = -2 Output: 4 ``` The subsequence `[7, 5, 3, 1]` uses the elements at indices 2, 4, 5 and 8, and each step subtracts 2. A fifth element would need a 9 before the 7 or a -1 after the last 1, and neither exists.

Overview: Given an integer array and a fixed difference, find the length of the longest subsequence in which each element minus the one before it equals that difference. Tests dynamic programming keyed by value, hash map lookups, and a linear-time approach that holds up for arrays of up to 100,000 elements.

GitHub logo
GitHub
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array arr and an integer difference, return the length of the longest subsequence of arr that is an arithmetic sequence in which the difference between adjacent elements equals difference.

A subsequence is a sequence that can be derived from arr by deleting some or no elements without changing the order of the remaining elements.

Function Signature

def longest_subsequence_with_difference(arr: list[int], difference: int) -> int:

Rules

  • "Adjacent" means consecutive in the subsequence: for every pair of neighbors, the later element minus the earlier one equals difference .
  • difference may be negative or zero. With a difference of zero, the subsequence consists of equal values.
  • A single element is a valid subsequence of length 1, so the answer is always at least 1.

Constraints

  • 1 <= len(arr) <= 10^5
  • -10^4 <= arr[i], difference <= 10^4

Examples

Input: arr = [1, 2, 3, 4], difference = 1
Output: 4

The whole array is the subsequence [1, 2, 3, 4], and each step adds 1.

Input: arr = [1, 3, 5, 7], difference = 1
Output: 1

No two elements differ by exactly 1, so the best is any single element.

Input: arr = [1, 5, 7, 8, 5, 3, 4, 2, 1], difference = -2
Output: 4

The subsequence [7, 5, 3, 1] uses the elements at indices 2, 4, 5 and 8, and each step subtracts 2. A fifth element would need a 9 before the 7 or a -1 after the last 1, and neither exists.