# O(n) Java solution

• ``````public class Solution {
public int minSubArrayLen(int s, int[] nums) {
int result = Integer.MAX_VALUE;
int spos = 0, epos = 0, sum = 0;

while(epos < nums.length) {
sum += nums[epos];
if(sum >= s) {
while(spos <= epos && sum >= s) {
int length = epos - spos + 1;
result = Math.min(result, length);
sum -= nums[spos++];
}
}
epos++;
}

return result == Integer.MAX_VALUE ? 0 : result;
}
}``````

• This post is deleted!

• This might sound noob but with your code, for test case s = 11, nums[1, 2, 3, 4, 5], though the minimum length you found is the correct answer, it doesn't represent [2, 4, 5], or does it? Can you explain? Thanks!

• It represents [3, 4, 5]. epos will end at 4 since 1 + 2 + 3 + 4 + 5 = 15 > 11, and then spos will start to increment until the sum is smaller than 11. So the minimum length will be recorded when spos is 2, which means it represents [3, 4, 5].

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.