Longest Arithmetic Subsequence LeetCode solution in Java
1class Solution {2public int longestArithSeqLength(int[] nums) {3int n = nums.length;4int longest = 0;5Map<Integer, Integer>[] dp = new HashMap[n];67for (int i = 0; i < n; i++) {8dp[i] = new HashMap<>();910for (int j = 0; j < i; j++) {11int diff = nums[i] - nums[j];12dp[i].put(diff, dp[j].getOrDefault(diff, 1) + 1);13longest = Math.max(longest, dp[i].get(diff));14}15}1617return longest;18}19}