✅99.55% Better than Other C++ Users|| Easy ||Concept Explained...!
Intuition\nWhen you first see this problem, you might think about how you can create a range of consecutive numbers starting from 1 using the given coins. The k
### Intuition\nWhen you first see this problem, you might think about how you can create a range of consecutive numbers starting from 1 using the given coins. The key insight here is to use a greedy approach to always use the smallest available coin to extend the range of consecutive numbers you can make. By sorting the coins and iterating through them, you can ensure that you are always using the optimal coin to extend your range.\n\n### Approach\n1. **Sort the Array:** Start by sorting the coins array. This allows you to consider the smallest coin first, ensuring that you build up the range of consecutive values in the most efficient way possible.\n2. **Initialize maxReach:** This variable keeps track of the maximum number of consecutive values you can make starting from 1.\n3. **Iterate through the coins:** For each coin, check if it can extend the range of consecutive values. If the current coin is greater than `maxReach + 1`, you can\'t make `maxReach + 1` and thus, you return `maxReach + 1`. If the current coin can extend the range, add it to `maxReach`.\n4. **Return the Result:** After processing all the coins, return `maxReach + 1`, as this represents the maximum number of consecutive values you can make.\n\n### Complexity\n- **Time complexity:** \\(O(n \\log n)\\) due to sorting the array of coins.\n- **Space complexity:** \\(O(1)\\) since we are using a constant amount of extra space for the `maxReach` variable and loop indices.\n\n### Code\nHere\'s the implementation in C++:\n\n```cpp\nclass Solution {\npublic:\n    int getMaximumConsecutive(vector<int>& coins) {\n        ios_base::sync_with_stdio(0);\n        sort(coins.begin(), coins.end());\n        int maxReach = 0;\n        int n = coins.size();\n        for (int i = 0; i < n; i++) {\n            if (coins[i] > maxReach + 1) {\n                return maxReach + 1;\n            } else {\n                maxReach += coins[i];\n            }\n        }\n        return maxReach + 1;\n    }\n};\n```\n\n### Explanation\n- **Sorting the Coins:** Sorting ensures that you always consider the smallest coin first.\n- **Updating maxReach:** For each coin, if it is less than or equal to `maxReach + 1`, you can add it to `maxReach` and extend the range. Otherwise, if it\'s larger, you cannot make the next consecutive number, so you return `maxReach + 1`.\n- **Result:** The final result is `maxReach + 1`, indicating the maximum number of consecutive values you can make starting from 1.\n\nThis solution is efficient and leverages the greedy algorithm to ensure that you are always making the optimal choice at each step.
['C++']