BEATS 100% users with java | simple approach
\n\n\n# Approach\n Describe your approach to solving the problem. \n- The algorithm uses a while loop to iterate from 1 to n (inclusive) and checks if the curre
![image.png](https://assets.leetcode.com/users/images/2c28542c-27dd-4461-bb74-f0f91a9826e2_1718518467.8376067.png)\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- The algorithm uses a while loop to iterate from 1 to n (inclusive) and checks if the current miss value can be obtained using the elements in the nums array.\n- If the current number in nums is less than or equal to the miss value, it is added to miss, and the index i is incremented to consider the next element.\n- If the current number in nums is greater than the miss value, it means a patch is required. In this case, the result is incremented, and miss is doubled.\n- This process continues until miss exceeds n, at which point the algorithm returns the final result.\n\n# Complexity\n- Time complexity:o(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\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 miss = 1;\n        int result = 0;\n        int i = 0;\n\n        while (miss <= n) {\n            if (i < nums.length && nums[i] <= miss) {\n                miss += nums[i];\n                i++;\n            } else {\n                miss += miss;\n                result++;\n            }\n        }\n\n        return result;\n    }\n}\n```
['Java']