A Sliding Window Problem II

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 256M

Author:
Problem types

AndyMan68 is back yet again with another sliding window problem! This time, he imagines a window of size W, sliding from the leftmost position in an array to the rightmost. He wants to ensure each window's elements sum to at least K. However, large windows are expensive! He wants to find the minimum window size such that this condition is met.

Formally, he wants to find the minimum value W such that all subarrays of size W have a sum of K or greater.

Help AndyMan68 solve this problem!

Input Specification

The first line contains two spaced integers N (1 \le N \le 10^5) and K (1 \le K \le \text{sum} (a_i)), representing the number of elements in the array and the minimum sum required for the sliding window.

The next line contains N spaced integers, where the i^{\text{th}} integer is a_i (0 \le a_i \le 10^5).

Output Specification

On a single line, output the minimum window size W required such that all windows of size W has a sum of at least K. You may assume that the minimum W will be greater than 0 and less than or equal to N.

Beware of integer overflow as the sum within a window can exceed the integer limit in some languages such as C++.

Sample Input 1

5 7
2 9 3 2 4

Output for Sample Input 1

3

Explanation of Output for Sample Input 1

The windows of size 3 can be seem below:

[2 \; 9 \; 3] \; 2 \; 4

2 \; [9 \; 3 \; 2] \; 4

2 \; 9 \; [3 \; 2 \; 4]

Each window has a sum of at least 7. A window size any smaller, such as a size 2, would not.

Sample Input 2

8 5
2 0 3 2 2 0 1 4

Output for Sample Input 2

4

Sample Input 3

6 3
3 3 3 3 3 3

Output for Sample Input 3

1

Comments

There are no comments at the moment.