Java with explanation
\n  /*\n    Idea:\n    Watch the case [1,5,10], n = 20\n    let r as the right side number value of continuous sum range [0, r]\n    initial r=0; and expected n
```\n  /*\n    Idea:\n    Watch the case [1,5,10], n = 20\n    let r as the right side number value of continuous sum range [0, r]\n    initial r=0; and expected next number nums[i] should be <= r+1, else need a patch = r+1;\n    if  nums[i]==r+1 then r will be r+(r+1)\n    if  nums[i]< r+1 then r will be r+nums[i];\n    i=0 numb[0] is 1, so need not patch and r=r+1= 1;\n    i=1,numb[1] is 5, not expected <=2\n         so need a patch =2;\n         with the patch, now r=r+2=3 and expected <=4, but numb[1] is 5\n         so need a patch =4;\n         with the patch, now r=r+4=7 and expected <=8,   numb[1] is 5 works now\n         so now r=r+5=12,  expected <=13,  move i to next\n     i=2,numb[2] is 10, need not patch\n         r=r+10=22> target 20 then break the loop.\n\n    Note \' 1 <= n <= 2^31 - 1\'\n    so the `r` of continuous sum range [0, r] should be a long type\n\n    Observe\n      - we must patch the expected number else continuous sum range [0, r] can not continue\n\n\n   O(m+logN) time, M is the length of nums. N is the give number n;\n   O(1) space\n  */\n  public static int minPatches(int[] nums, int n) {\n    int patches = 0, N = nums.length;\n    long r = 0; // need use long to avoid overflow\n    int i = 0;\n    while (r < n) {\n      if (i < N && nums[i] > r + 1 || i == N) {\n        patches++; // patch = r+1\n        r = 2 * r + 1;\n      } else {\n        r = r + nums[i];\n        i++;\n      }\n    }\n    return patches;\n  }\n```
[]