Longest Contiguous Sub Array Less Than Or Equal To K
Find the length of longest contiguous sub-array where the sum of the elements in subarray is less than or equal to "k".
def lengthLongestContiguousSubArray(arr, k):
left, right = 0, 0
total, length = 0, 0
while (right < len(arr)):
total += arr[right]
while (total > k and left < right):
total -= arr[left]
left += 1
length = max(length, right - left + 1)
right += 1
return length
print(lengthLongestContiguousSubArray([3,1,2,1], 4))
Time complexity: $O(n)$