Class Solution
java.lang.Object
g1201_1300.s1218_longest_arithmetic_subsequence_of_given_difference.Solution
1218 - Longest Arithmetic Subsequence of Given Difference.<p>Medium</p>
<p>Given an integer array <code>arr</code> and an integer <code>difference</code>, return the length of the longest subsequence in <code>arr</code> which is an arithmetic sequence such that the difference between adjacent elements in the subsequence equals <code>difference</code>.</p>
<p>A <strong>subsequence</strong> is a sequence that can be derived from <code>arr</code> by deleting some or no elements without changing the order of the remaining elements.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> arr = [1,2,3,4], difference = 1</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> The longest arithmetic subsequence is [1,2,3,4].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> arr = [1,3,5,7], difference = 1</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> The longest arithmetic subsequence is any single element.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> arr = [1,5,7,8,5,3,4,2,1], difference = -2</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> The longest arithmetic subsequence is [7,5,3,1].</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= arr.length <= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> <= arr[i], difference <= 10<sup>4</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
longestSubsequence
public int longestSubsequence(int[] arr, int difference)
-