PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000162. Đoạn ngắn nhất có tổng đúng K (Shortest Subarray With Sum K)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đoạn ngắn nhất có tổng đúng K (Shortest Subarray With Sum K)
Shortest Subarray With Sum K
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array of integers , numbered from 1. A nonempty contiguous interval contains all elements from through and has length . Find the minimum length of an interval whose sum is exactly . Print -1 if none exists.
Input
The first line contains two integers . The second line contains integers .
Output
Print the minimum qualifying length, or -1 if none exists.
Subtasks
- Subtask 1 (20 points): ; for all ; .
- Subtask 2 (30 points): ; for all ; .
- Subtask 3 (50 points): ; for all ; .
Example 1
Input
4 3
2 -1 2 3
Output
1
Explanation
Interval [4,4] sums to 3 and has length 1, the smallest possible nonempty length.
Example 2
Input
3 0
1 -1 2
Output
2
Explanation
No element is zero. Interval [1,2] sums to zero, so the shortest length is 2.