c++ sol | Runtime 4ms | faster than 91.33%
Let\'s consider an example: \n\nLet\'s say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that all sums in the range [1,100] are possible.
**Let\'s consider an example: **\n\nLet\'s say the input is nums = [1, 2, 4, 13, 43] and n = 100. We need to ensure that all sums in the range [1,100] are possible.\nUsing the given numbers 1, 2 and 4, we can already build all sums from 0 to 7, i.e., the range [0,8). But we can\'t build the sum 8, and the next given number (13) is too large. So we insert 8 into the array. Then we can build all sums in [0,16).\n\nDo we need to insert 16 into the array? No! We can already build the sum 3, and adding the given 13 gives us sum 16. We can also add the 13 to the other sums, extending our range to [0,29).\n\nAnd so on. The given 43 is too large to help with sum 29, so we must insert 29 into our array. This extends our range to [0,58). But then the 43 becomes useful and expands our range to [0,101). At which point we\'re done.\n\n\n```\nclass Solution {\npublic:\n    int minPatches(vector<int>& nums, int n) {\n\t// To play safe from int overflow\n        unsigned missing = 1, i = 0, ans = 0;\n\t\t// we will run this loop in the range of [1,n]\n        while(missing <= n){\n\t\t// First check is to stay safe from runtime error\n\t// secondly we\'ll check if the each element of the array is smaller than or equals to the missing one\n\t\t// then we\'ll keep that element else we\'ll keep the twice of missing because of the concept I\'ve written above\n            if(i < nums.size() && nums[i] <= missing){\n                missing += nums[i];\n                i++;\n            }else{\n\t\t\t// if we got our nums[i] greater than the missing one then we can\'t keep it \n\t\t\t// so with this conclusion we can increase the count of our ans\n                missing += missing;\n                ans++;\n            }  \n        }\n        return ans;\n    }\n};\n```
['C']