Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Longest Arithmetic Subsequence of Given Difference

Updated
2 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1218" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

Given an integer array arr and an integer difference, return the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence 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. Example 1: Input: arr = [1,2,3,4], difference = 1 Output: 4 Explanation: The longest arithmetic subsequence is [1,2,3,4]. Example 2: Input: arr = [1,3,5,7], difference = 1 Output: 1 Explanation: The longest arithmetic subsequence is any single element. Example 3: Input: arr = [1,5,7,8,5,3,4,2,1], difference = -2 Output: 4 Explanation: The longest arithmetic subsequence is [7,5,3,1]. Constraints: 1 <= arr.length <= 105 -104 <= arr[i], difference <= 104

Explanation

Here's the solution to find the longest arithmetic subsequence with a given difference:

  • Dynamic Programming with Hash Map: Utilize a hash map (dictionary in Python) to store the length of the longest arithmetic subsequence ending at each number encountered so far.
  • Iterative Approach: Iterate through the input array, and for each number, check if its predecessor (number - difference) exists in the hash map. If it does, extend the arithmetic subsequence length by 1. Otherwise, start a new arithmetic subsequence of length 1.
  • Maximum Length Tracking: Maintain a variable to keep track of the maximum length found so far.

  • Runtime Complexity: O(n), where n is the length of the input array. Storage Complexity: O(n) in the worst case, where n is the length of the input array (if all elements are distinct).

Code

    def longestArithSeqLength(arr: list[int], difference: int) -> int:
    """
    Finds the length of the longest arithmetic subsequence in arr with the given difference.

    Args:
        arr: The input integer array.
        difference: The difference between adjacent elements in the subsequence.

    Returns:
        The length of the longest arithmetic subsequence.
    """

    dp = {}  # Key: number, Value: length of longest arithmetic subsequence ending at that number
    max_length = 0

    for num in arr:
        if num - difference in dp:
            dp[num] = dp[num - difference] + 1
        else:
            dp[num] = 1
        max_length = max(max_length, dp[num])

    return max_length

More from this blog

C

Chatmagic blog

2894 posts