class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int firstPos = 0, sum = 0, minLength = INT_MAX;
for(int i = 0; i<nums.size(); i++) {
sum += nums[i];
while(sum >= s) {
minLength = min(minLength, i  firstPos + 1);
sum = nums[firstPos++];
}
}
return minLength == INT_MAX? 0 : minLength;
}
};
C++ Simple O(n) solution


I hava a similar solution public class Solution { public int minSubArrayLen(int s, int[] nums) { int length=nums.length; int minlength=Integer.MAX_VALUE,sublength=0; int sum=0; for(int i=0;i<length;) { while(sum<s&&i<length) { sum+=nums[i++]; sublength++; } while(sum>=s) { minlength=Math.min(minlength,sublength); sum=nums[isublength]; sublength; } } return minlength<Integer.MAX_VALUE?minlength:0; }
}