Given an integer array, find a subarray where the sum of numbers is in a given interval. Your code should return the number of possible answers. (The element in the array should be positive)
Example
Given[1,2,3,4]and interval =[1,3], return4. The possible answers are:
publicintsubarraySumII(int[] A,int start,int end) {if (A ==null||A.length==0) {return0; }int n =A.length;// calculate prefix sum, becasue the array contains all positive numbers, // so the prefix sum array will be in increasing order.int[] prefixSum =newint[n +1];for (int i =1; i <= n; i++) { prefixSum[i] = prefixSum[i -1] +A[i -1]; }// do binary search in range sumint count =0;for (int i =1; i <= n; i++) {// use this instead of a loop to find the proper j in the range [0 ~ (i - 1)]// will lower the T from O(n^2) to O(nlogn)int right =find(prefixSum, i -1, prefixSum[i] - start +1);int left =find(prefixSum, i -1, prefixSum[i] - end); count += right - left; }return count;}// this function is use to do binary search for value in prefix sum array,// which smaller than 'value' ends at index 'last'privateintfind(int[] prefixS,int last,int value) {if (prefixS[0] >= value) {return0; }if (prefixS[last] < value) {return last +1; }int l =0;int r = last;while (l +1< r) {int mid = l + (r - l) /2;if (prefixS[mid] < value) { l = mid; } else { r = mid; } }if (prefixS[r] < value) {return r +1; } else {return l +1; }}