C++/Java/Python with picture
The intuition for this one is cool. Got a few TLE before figuring it out.\n\nThis is the realization: if we reached number i, that means that we can make all va
The intuition for this one is cool. Got a few TLE before figuring it out.\n\nThis is the realization: if we reached number `i`, that means that we can make all values `0...i`. So, if we add a coin with value `j`, we can also make values `i+1...i+j`.\n\nThe only case when we can have a gap is the coin value is greater than `i + 1 `. So we sort the coins to make sure we process smaller coins first.\n\nFor example, let\'s assume that we can make all values `0...7`. Adding a nickel allows us to make values `8..12` (`7+1...7+5`).\n\n![image](https://assets.leetcode.com/users/images/f1280227-dec4-4514-ab56-e68b15b5bf7b_1616257807.8662581.png)\n\n**C++**\n```cpp\nint getMaximumConsecutive(vector<int>& c) {\n    sort(begin(c), end(c));\n    int max_val = 1;\n    for (auto i = 0; i < c.size() && c[i] <= max_val; ++i)\n        max_val += c[i];\n    return max_val;\n}  \n```\n**Java**\n```java\npublic int getMaximumConsecutive(int[] c) {\n    Arrays.sort(c);\n    int max_val = 1;\n    for (int i = 0; i < c.length && c[i] <= max_val; ++i)\n        max_val += c[i];\n    return max_val;        \n}\n```\n**Python 3**\n```python\nclass Solution:\n    def getMaximumConsecutive(self, coins: List[int]) -> int:\n        max_val = 1\n        for c in sorted(coins):\n            if c > max_val:\n                break\n            max_val += c\n        return max_val\n```
[]