1218. Longest Arithmetic Subsequence of Given Difference LeetCode Solution
In this guide, you will get 1218. Longest Arithmetic Subsequence of Given Difference LeetCode Solution with the best time and space complexity. The solution to Longest Arithmetic Subsequence of Given Difference problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Longest Arithmetic Subsequence of Given Difference solution in C++
- Longest Arithmetic Subsequence of Given Difference solution in Java
- Longest Arithmetic Subsequence of Given Difference solution in Python
- Additional Resources
Problem Statement of Longest Arithmetic Subsequence of Given Difference
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
Complexity Analysis
- Time Complexity:
- Space Complexity:
1218. Longest Arithmetic Subsequence of Given Difference LeetCode Solution in C++
class Solution {
public:
int longestSubsequence(vector<int>& arr, int difference) {
int ans = 0;
unordered_map<int, int> lengthAt;
for (const int a : arr) {
if (const auto it = lengthAt.find(a - difference); it != lengthAt.cend())
lengthAt[a] = it->second + 1;
else
lengthAt[a] = 1;
ans = max(ans, lengthAt[a]);
}
return ans;
}
};
/* code provided by PROGIEZ */
1218. Longest Arithmetic Subsequence of Given Difference LeetCode Solution in Java
class Solution {
public int longestSubsequence(int[] arr, int difference) {
int ans = 0;
Map<Integer, Integer> lengthAt = new HashMap<>();
for (final int a : arr) {
lengthAt.put(a, lengthAt.getOrDefault(a - difference, 0) + 1);
ans = Math.max(ans, lengthAt.get(a));
}
return ans;
}
}
// code provided by PROGIEZ
1218. Longest Arithmetic Subsequence of Given Difference LeetCode Solution in Python
class Solution:
def longestSubsequence(self, arr: list[int], difference: int) -> int:
ans = 0
lengthAt = {}
for a in arr:
lengthAt[a] = lengthAt.get(a - difference, 0) + 1
ans = max(ans, lengthAt[a])
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.