HashMap ➕ Dynamic Programming
\npublic class Solution {\n    public int lengthOfLongestSubsequence(List<Integer> nums, int target) {\n        HashMap<Integer, Integer> dp = new HashMap<>();\
```\npublic class Solution {\n    public int lengthOfLongestSubsequence(List<Integer> nums, int target) {\n        HashMap<Integer, Integer> dp = new HashMap<>();\n        dp.put(0, 0);\n        for (int num : nums) {\n            // Iterate in reverse order to avoid using the same number multiple times\n            for (int t = target; t >= num; t--) {\n                // If t - num is in dp, we can create a new subsequence with sum t\n                if (dp.containsKey(t - num)) {\n                    // Update dp[t] to be the maximum of its current value and dp[t - num] + 1\n                    dp.put(t, Math.max(dp.getOrDefault(t, 0), dp.get(t - num) + 1));\n                }\n            }\n        }\n        if (dp.containsKey(target)) {\n            return dp.get(target);\n        }\n        return -1;\n    }\n}\n\n```\n# Upvoting is much Appreciated
['Dynamic Programming', 'Ordered Map', 'Java']