Zepruv › Coding problems

Longest Arithmetic Subsequence

Medium · Array, Hash Table, Binary Search, Dynamic Programming

Given an array nums of integers, return the length of the longest arithmetic subsequence in nums.
Note that:

Example 1

Input: nums = [3,6,9,12]
Output: 4

The whole array is an arithmetic sequence with steps of length = 3.

Example 2

Input: nums = [9,4,7,2,10]
Output: 3

The longest arithmetic subsequence is [4,7,10].

Example 3

Input: nums = [20,1,15,3,10,5,8]
Output: 4

The longest arithmetic subsequence is [20,15,10,5].

Constraints

Starter code (python)

class Solution(object):
    def longestArithSeqLength(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        

Related problems

Previous: Divisor Game · Next: Maximum Difference Between Node and Ancestor · All problems

Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.