# Subarray Sum Less Than K (IB)

## 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)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://joshualbarb.gitbook.io/leetcode-problems/sliding-window/subarray-sum-less-than-k.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
