> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/sliding-window/subarray-sum-less-than-k.md).

# Subarray Sum Less Than K (IB)

Sliding Window

## Problem

Your are given an array of positive integers `nums`.

Count and print the number of (contiguous) subarrays where the sum of all the elements in the subarray is less than `k`.

{% hint style="info" %}
For example:

```
 Input: nums = [2, 5, 6], k = 10
 Output: 4
```

{% endhint %}

## Solution

```
class Solution:
    def solve(self, nums, k):
        start = 0
        sums = 0
        count = 0
        
        for i in range(len(nums)):
            sums+=nums[i]
            while sums >= k:
                sums-=nums[start]
                start+=1
            count+=i-start+1
        return count
        
#Time: O(n)
#Space: O(n)
```
