Go DP Solution with explanation
Intuition\nI couldn\'t come up with solution during contest but looking into solutions of others, I came up with this approach. The no of variables could be cle
# Intuition\nI couldn\'t come up with solution during contest but looking into solutions of others, I came up with this approach. The no of variables could be clearly optimized,but I leave them for the sake of better clarity. Obviously this is not the best solution space complexity wise, but still it gives me a starting point to optimize on that.\n\n# Approach\nFirstly, it clearly makes sense to sort the array becuase finding consecutives would get far easier.\n\nNext is that we may store the maximum sequence we may achieve on a given number. For this we can have a map which stores as follows-\n\n```\nkey -> value\nwhere key is the number and,\nvalue is the maximum consecutive subsequence we can have ending on key\n\n```\nFor instance\n\n```\n7 -> 5 means \nthere exist a subsequqnce ending on 7 with size 5 i.e (3.4.5.6.7)\n```\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\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```\nfunc maxSelectedElements(nums []int) int {\n    slices.Sort(nums)\n    \n    //This will store what is the maximum size we may achieve ending on this element\n    // for example, values would be like\n    // 7 -> 5, which means that the there exist a consecutive sequqnce ending on 7 with size 5 {3,4,5,6,7}\n    until := make(map[int]int)\n\n    until[nums[0]] = 1\n    until[nums[0]+1] = 1\n\n    res := 1\n    for i := 1; i < len(nums); i++ {\n        curr := nums[i]\n        incr := curr+1\n        \n        if val, ok := until[incr-1]; ok {\n            until[incr] = val+1\n            res = int(math.Max(float64(res), float64(val+1)))\n        } else {\n            until[incr] = 1\n        }\n\n        if val, ok := until[curr-1]; ok {\n            until[curr] = val+1\n            res = int(math.Max(float64(res), float64(val+1)))\n        } else {\n            until[curr] = 1\n        }\n    }\n\n    return res\n}\n```
['Go']