Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Patching Array

XPChallenge Points: 30
levelLevel: Hard

You are given a sorted array of positive integers called nums, along with a target number n.

Your task is to ensure that every number from 1 to n can be formed as the sum of some elements in the array (each element may be used once).
If certain values cannot be created using the existing numbers, you may insert additional numbers (patches) into the array.

Your goal is to determine the smallest number of patches needed so that all values in the range [1, n] become representable.

Example 1:

Input: nums = [2,4], n = 10

Output: 2

Explanation:

Missing values like 1 and 3 require us to add numbers. Adding 1 and 3 allows all sums from 1 to 10.

Example 2:

Input: nums = [1,2,8], n = 15

Output: 1

Explanation:

We can already form values up to 3 and 9 using existing numbers. Adding 4 makes the entire range up to 15 reachable.

Example 3:

Input: nums = [1,1,3], n = 7

Output: 0

Explanation:

We can already build all numbers from 1 to 7.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result