Python / Kotlin / JavaScript Map O(n) Time O(n) Space
Intuition\n Describe your first thoughts on how to solve this problem. \nSimilar to 128. Longest Consecutive Sequence with some additional logic.\n- We put all 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimilar to [128. Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/description/) with some additional logic.\n- We put all the numbers into a map (number to count of number) and look for the start of a sequence. The start of a sequence is a number where the previous two numbers don\'t exist in the map (previous 2 because n - 2 could be increased to become the start of the sequence)\n- Then we start processing. Instead of just looking for the next number in the map, we have to consider whether we can increase the current number to continue the sequence if there\'s a break. Consider the following sequence:\n![image.png](https://assets.leetcode.com/users/images/afebcd4b-d5a1-4447-beec-f6f9d9b7a422_1708301168.2314591.png)\n\n\n  - If we have a duplicate number, we can safely increase\n  ![image.png](https://assets.leetcode.com/users/images/253ef243-522c-422c-aef0-035e934fdbe0_1708301197.0194018.png)\n\n  - If some previous number was increased, we can safely increase\n  ![image.png](https://assets.leetcode.com/users/images/42741d20-8cf8-4bea-a1eb-99ccb8611154_1708301227.0399194.png)\n  \n\n  - We can increase a continuous block of numbers (but obviously, this changes the length of the current sequence)\n  ![image.png](https://assets.leetcode.com/users/images/eb974668-17bd-483a-a099-ac4b188fc63d_1708300740.908935.png)\n    Note that it doesn\'t matter if the ```2``` was increased, we still would create a gap, so we just need to track the current continugous block of numbers\n\n- If the current and next numbers don\'t exist, we stop processing since there\'s no way to continue the sequence.\n- As we process each number, mark it in the map so we know not to process it again. This means we\'ll process each sequence starting from the beginning, and skip over all other numbers, so we\'ll visit each number at most twice.\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n- Initialize a map containing the counts of all numbers\n- Loop through the num and count of each entry in the map. If we\'ve already processed this num, we\'ll mark the count 0 so we can skip it.\n- If the num is the start of a sequence (we haven\'t marked it and the two numbers before it don\'t exist), begin processing\n  - Loop to infinity starting from the current num. Track the current streak along with the count of contiguous numbers\n  - We can safely increase the current num if it exists and we either we have multiples of it, or we were able to increase the previous number\n  - If the current and next numbers aren\'t in the map, break\n  - If the next number doesn\'t exist and we can\'t safely increase the current number, we\'ve found a situation where we need to increase the entire contiguous block. Our streak restarts to be the count of the current contiguous block because it won\'t be able to connect with the previous block (if one exists)\n  - If the current number is in the map, mark its count as 0 so we can skip it if we see it again in the outer loop.\n  - If the current streak is > the max streak, update the max streak\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n``` python []\nfrom collections import Counter\nclass Solution:\n    def maxSelectedElements(self, nums: List[int]) -> int:\n        counts = Counter(nums)\n\n        max_streak = 1\n        for num, count in counts.items():\n            if not count or num - 1 in counts or num - 2 in counts: continue\n\n            streak, consecutive = 0, 0\n            prev_increase = False\n\n            for curr in range(num, 2_000_000):\n                streak += 1\n                max_streak = max(max_streak, streak)\n\n                has_curr, has_next = curr in counts, curr + 1 in counts\n                safely_increase = has_curr and (prev_increase or counts[curr] > 1)\n\n                if not has_next and not has_curr: break\n\n                if not has_next and not safely_increase: streak = consecutive\n\n                prev_increase = safely_increase if has_next else True\n\n                if has_curr: counts[num] = 0\n\n                consecutive = consecutive + 1 if has_curr else 0\n        \n        return max_streak\n```\n``` kotlin []\nclass Solution {\n    fun maxSelectedElements(nums: IntArray): Int {\n        val counts = nums.toList().groupingBy { it }.eachCount().toMutableMap()\n\n        var maxStreak = 1\n        for ((num, count) in counts) {\n            if (count == 0 || num - 1 in counts || num - 2 in counts) continue\n\n            var (streak, consecutive) = listOf(0, 0)\n            var prevIncrease = false\n\n            for (curr in num..2_000_000) {\n                streak += 1\n                maxStreak = max(maxStreak, streak)\n\n                val (hasCurr, hasNext) = listOf(curr in counts, curr + 1 in counts)\n                val safelyIncrease = hasCurr && (prevIncrease || (counts[curr] ?: 0) > 1)\n\n                if (!hasNext && !hasCurr) break\n\n                if (!hasNext && !safelyIncrease) streak = consecutive\n\n                prevIncrease = if (hasNext) safelyIncrease else true\n\n                if (hasCurr) counts[curr] = 0\n\n                consecutive = if (hasCurr) consecutive + 1 else 0\n            }\n        }\n\n        return maxStreak\n    }\n}\n```\n``` javascript []\nvar maxSelectedElements = function(nums) {\n  const counts = nums.reduce((counts, num) => {\n    counts.set(num, counts.get(num) + 1 || 1);\n    return counts;\n  }, new Map());\n\n  let max = 1;\n  for (const [num, count] of counts) {\n    if (count === 0 || counts.has(num - 1) || counts.has(num - 2)) continue;\n    let [streak, consecutive] = [0, 0];\n    let previousIncrease = false;\n    \n    for (let curr = num; true; curr += 1) {\n      streak += 1;\n      max = Math.max(max, streak);\n\n      const [hasCurr, hasNext] = [counts.has(curr), counts.has(curr + 1)];\n      const safelyIncrease = hasCurr && (previousIncrease || counts.get(curr) > 1);\n\n      if (!hasNext && !hasCurr) break;\n\n      if (!hasNext && !safelyIncrease) streak = consecutive;\n\n      previousIncrease = hasNext ? safelyIncrease : true;\n\n      if (hasCurr) counts.set(curr, 0);\n      consecutive = hasCurr ? consecutive + 1 : 0;\n    }\n  }\n\n  return max;\n};\n```
['Hash Table', 'Python3', 'Kotlin', 'JavaScript']