C++ | | Easy Solution
Intuition\n###### Given a sorted array nums and an integer n, determine the minimum number of additional elements needed so that any number from 1 to n can be f
# Intuition\n###### Given a sorted array `nums` and an integer `n`, determine the minimum number of additional elements needed so that any number from 1 to `n` can be formed as the sum of some elements in the array.\n\n---\n\n\n# Approach\n\n### Approach\n1. **Initialization**: \n   - Set `sum = 0` to track the maximum sum achievable.\n   - Set `res = 0` to count the number of patches needed.\n   - Use `i = 0` to iterate through `nums`.\n\n2. **Iteration**:\n   - While `sum < n`, check if the next element in `nums` can extend `sum`:\n     - If `nums[i] <= sum + 1`, add `nums[i]` to `sum` and increment `i`.\n     - If `nums[i] > sum + 1`, add a patch (`sum + 1`) to cover the gap and increment `res`.\n\n\n---\n\n\n\n# Complexity\n##### Time complexity: $$O(n)$$\n\n##### Space complexity: $$O(1)$$\n\n---\n\n\n\n# Code\n```\nclass Solution {\npublic:\n    int minPatches(vector<int>& nums, int n) {\n        long long sum = 0;\n        int res = 0;\n        int i = 0;\n\n        while (sum < n) {\n            if (i < nums.size() && nums[i] <= sum + 1) {\n                sum += nums[i];\n                i++;\n            } else {\n                sum += (sum + 1);\n                res++;\n            }\n        }\n\n        return res;\n    }\n};\n```
['C++']