I'm writing an algorithm that needs to find the length of smallest subarray that its sum is larger than given k parameter.
This is the function I wrote so far:
public static int smallestSub (int [] a, int k) {
int currSum = a[0];
int start = 0;
int minLen = a.length + 1;
int currLen = 1;
for (int i = 1; i < a.length; i++) {
currSum += a[i];
currLen++;
while (currSum - a[start] > k && start < i) {
currSum -= a[start];
start++;
currLen--;
}
if (currSum > k) {
minLen = Math.min(currLen, minLen);
}
}
return minLen;
}
My question is: is the complexity of this algorithm O(n^2)?
I'm asking since while loop depends on for loop.