**Physical Address**

304 North Cardinal St.

Dorchester Center, MA 02124

Given a sorted integer array `nums`

and an integer `n`

, add/patch elements to the array such that any number in the range `[1, n]`

inclusive can be formed by the sum of some elements in the array.

Return *the minimum number of patches required*.

**Example 1:**

**Input:** nums = [1,3], n = 6
**Output:** 1
Explanation:
Combinations of nums are [1], [3], [1,3], which form possible sums of: 1, 3, 4.
Now if we add/patch 2 to nums, the combinations are: [1], [2], [3], [1,3], [2,3], [1,2,3].
Possible sums are 1, 2, 3, 4, 5, 6, which now covers the range [1, 6].
So we only need 1 patch.

**Example 2:**

**Input:** nums = [1,5,10], n = 20
**Output:** 2
Explanation: The two patches can be [2, 4].

**Example 3:**

**Input:** nums = [1,2,2], n = 5
**Output:** 0

**Constraints:**

`1 <= nums.length <= 1000`

`1 <= nums[i] <= 10`

^{4}`nums`

is sorted in**ascending order**.`1 <= n <= 2`

^{31}- 1

```
class Solution {
public:
int minPatches(vector<int>& nums, int n) {
int cnt=0,i=0;
long long maxNum=0;
while (maxNum<n){
if (i<nums.size() && nums[i]<=maxNum+1)
maxNum+=nums[i++];
else{
maxNum+=maxNum+1;cnt++;
}
}
return cnt;
}
};
```

```
class Solution:
def merge(self, intervals):
intervals.sort(key=lambda x: x[0])
merged = []
for interval in intervals:
if not merged or merged[-1][1] < interval[0] - 1:
merged.append(interval)
else:
merged[-1][1] = max(merged[-1][1], interval[1])
return merged
def minPatches(self, nums, n):
ints, patches = [[0,0]], 0
for num in nums:
ints = self.merge(ints + [[i+num, j+num] for i,j in ints])
while ints[0][1] < n:
ints = self.merge(ints + [[i+ints[0][1]+1, j+ints[0][1]+1] for i,j in ints])
patches += 1
return patches
```

```
public static int minPatches(int[] nums, int n) {
long max = 0;
int cnt = 0;
for (int i = 0; max < n;) {
if (i >= nums.length || max < nums[i] - 1) {
max += max + 1;
cnt++;
} else {
max += nums[i];
i++;
}
}
return cnt;
}
```

In our experience, we suggest you solve this Patching Array LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Patching Array LeetCode Solution

I hope this Patching Array LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

**More Coding Solutions >>**