Simple solution in Java!! With dry run ✅✅✅✅
Intuition\nTo ensure all numbers from 1 to n can be formed using a given array nums, we can add the smallest missing number that cannot be formed so far. This m
## Intuition\nTo ensure all numbers from 1 to `n` can be formed using a given array `nums`, we can add the smallest missing number that cannot be formed so far. This minimizes the number of patches needed.\n\n## Approach\n1. **Initialize**: Start with the smallest missing number `patch` set to 1 and counters for total patches and index.\n2. **Iterate**: \n   - If the current number in `nums` can be used to form `patch`, add it to `patch`.\n   - If it can\'t, add `patch` itself to form the next number, increment total patches.\n3. **Repeat**: Continue until all numbers up to `n` can be formed.\n\n## Dry Run\nExample:\n- `nums = [1, 3]`, `n = 6`\n\n| Step | `patch` | `index` | `total` | Action                          | New `patch` |\n|------|---------|---------|---------|---------------------------------|-------------|\n| 1    | 1       | 0       | 0       | Use `nums[0]`                   | 2           |\n| 2    | 2       | 1       | 0       | Add `patch` itself (missing 2)  | 4           |\n| 3    | 4       | 1       | 1       | Use `nums[1]`                   | 7           |\n\nFinal number of patches required is 1.\n\n# Complexity\n- Time complexity: O(log N) cause array is already sorted.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1), nom extra space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int minPatches(int[] nums, int n) {\n        long patch = 1;\n        int total = 0;\n        int index = 0;\n\n        while(patch <= n)\n        {\n            if(index < nums.length && patch >= nums[index])\n            {\n                patch += nums[index];\n                index++;\n            }\n            else\n            {\n                patch += patch;\n                total++;\n            }\n        }\n\n        return total;\n    }\n}\n```\n\n![image.png](https://assets.leetcode.com/users/images/8838dbee-cc3f-42bb-a931-0124376ea6f2_1718512251.621657.png)\n
['Java']