------
## 最终结论
```python
def longestSubsequence(arr, difference):
    dp = {}
    max_length = 0

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

    return max_length

# Example usage:
arr1 = [1, 2, 3, 4]
difference1 = 1
print(longestSubsequence(arr1, difference1))  # Output: 4

arr2 = [1, 3, 5, 7]
difference2 = 1
print(longestSubsequence(arr2, difference2))  # Output: 1

arr3 = [1, 5, 7, 8, 5, 3, 4, 2, 1]
difference3 = -2
print(longestSubsequence(arr3, difference3)) # Output: 4
```