# 1918. Kth Smallest Subarray Sum

<https://leetcode.com/problems/kth-smallest-subarray-sum>

## Description

Given an integer array `nums` of length `n` and an integer `k`, return *the* `kth` ***smallest subarray sum**.*

A **subarray** is defined as a **non-empty** contiguous sequence of elements in an array. A **subarray sum** is the sum of all elements in the subarray.

**Example 1:**

```
**Input:** nums = [2,1,3], k = 4
**Output:** 3
**Explanation:** The subarrays of [2,1,3] are:
- [2] with sum 2
- [1] with sum 1
- [3] with sum 3
- [2,1] with sum 3
- [1,3] with sum 4
- [2,1,3] with sum 6 
Ordering the sums from smallest to largest gives 1, 2, 3, 3, 4, 6. The 4th smallest is 3.
```

**Example 2:**

```
**Input:** nums = [3,3,5,5], k = 7
**Output:** 10
**Explanation:** The subarrays of [3,3,5,5] are:
- [3] with sum 3
- [3] with sum 3
- [5] with sum 5
- [5] with sum 5
- [3,3] with sum 6
- [3,5] with sum 8
- [5,5] with sum 10
- [3,3,5], with sum 11
- [3,5,5] with sum 13
- [3,3,5,5] with sum 16
Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, 10, 11, 13, 16. The 7th smallest is 10.
```

**Constraints:**

* `n == nums.length`
* `1 <= n <= 2 * 104`
* `1 <= nums[i] <= 5 * 104`
* `1 <= k <= n * (n + 1) / 2`

## ac

```java
```


---

# 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://jaywin.gitbook.io/leetcode/solutions/1918-kth-smallest-subarray-sum.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.
