|
| 1 | +# Intuition |
| 2 | +<!-- Describe your first thoughts on how to solve this problem. --> |
| 3 | +A simple use of hashmap. |
| 4 | + |
| 5 | + |
| 6 | +# Approach |
| 7 | +<!-- Describe your approach to solving the problem. --> |
| 8 | +Create a hashmap which will store numbers with their respective series possible till now, and for each numbers check if their previous elements were present in array or not, if yes then increse their count, otherwise let it be 1. |
| 9 | + |
| 10 | +Let me explain by a example : a = [1,5,7,8,5,3,4,2,1], difference = -2 |
| 11 | +Let max store the maximum length and initially its value is 0. |
| 12 | + |
| 13 | +Star with first element : 1 |
| 14 | +As ( 1 - (-2 ) ) isn't present in hashmap ( since it is the first element in our hashmap), so its length ( 0 + 1) = 1 |
| 15 | +Included in hashmap : (1, 1) |
| 16 | +As 1 > max : max = 1 |
| 17 | + |
| 18 | +Second element : 5 |
| 19 | +As ( 5 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1 |
| 20 | +Included in hashmap : (5, 1) |
| 21 | +As 1 isn't greater than max, so no change in max |
| 22 | + |
| 23 | +Third element : 7 |
| 24 | +As ( 7 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1 |
| 25 | +Included in hashmap : (7, 1) |
| 26 | +As 1 isn't greater than max, so no change in max |
| 27 | + |
| 28 | +Fourth element : 8 |
| 29 | +As ( 8 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1 |
| 30 | +Included in hashmap : (8, 1) |
| 31 | +As 1 isn't greater than max, so no change in max |
| 32 | + |
| 33 | +Fifth element : 5 |
| 34 | +As ( 5 - (-2) ) = (7) is present in hashmap, so its length = ( length(7) + 1) = ( 1 + 1) = 2 |
| 35 | +Included in hashmap : (5, 2) |
| 36 | +As 2 > max : max = 2 |
| 37 | + |
| 38 | +Sixth element : 3 |
| 39 | +As ( 3 - (-2) ) = (5) is present in hashmap, so its length = ( length(5) + 1 ) = ( 2 + 1) = 3 |
| 40 | +Included in hashmap : (3, 3) |
| 41 | +As 3 > max : max = 3 |
| 42 | + |
| 43 | +Seventh element : 4 |
| 44 | +As ( 4 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1 |
| 45 | +Included in hashmap : (4, 1) |
| 46 | +As 1 isn't greater than max, so no change in max |
| 47 | + |
| 48 | +Eigth element : 2 |
| 49 | +As ( 2 - (-2) ) = (4) is present in hashmap, so its length = ( length(4) + 1 ) = ( 1 + 1) = 2 |
| 50 | +Included in hashmap : (2, 2) |
| 51 | +As 2 isn't greater than max, so no change in max |
| 52 | + |
| 53 | +Ninth element : 1 |
| 54 | +As (1 - (-2) ) = (3) is present in hashmap, so its length = ( length(3) + 1 ) = ( 3 + 1) = 4 |
| 55 | +Included in hashmap : (1, 4) |
| 56 | +As 4 > max : max = 4 |
| 57 | + |
| 58 | +After iterating over all numbers, answer is received as max. |
| 59 | + |
| 60 | + |
| 61 | +# Complexity |
| 62 | +- Time complexity: |
| 63 | +<!-- Add your time complexity here, e.g. $$O(n)$$ --> |
| 64 | + |
| 65 | +- Space complexity: |
| 66 | +<!-- Add your space complexity here, e.g. $$O(n)$$ --> |
| 67 | + |
| 68 | +# Code |
| 69 | +``` |
| 70 | +class Solution { |
| 71 | + public int longestSubsequence(int[] arr, int difference) { |
| 72 | +
|
| 73 | + int max = 0; |
| 74 | + HashMap<Integer, Integer> m = new HashMap<>(); |
| 75 | + for( int i : arr){ |
| 76 | + m.put(i, m.getOrDefault( i - difference, 0) + 1); // if the previous possible element of the sequence is present already in hashmap then add thats length to the current element length + 1, otherwise 1 |
| 77 | + max = Math.max( max, m.get(i)); // updating max value if length is greater than max |
| 78 | + } |
| 79 | + return max; |
| 80 | + |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
0 commit comments