PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000165. Đoạn dài nhất có tổng đúng K (Longest 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 dài nhất có tổng đúng K (Longest Subarray With Sum K)
Longest 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 maximum length of an interval whose sum is exactly . Print 0 if none exists.
Input
The first line contains two integers . The second line contains integers .
Output
Print the maximum qualifying length, or 0 if none exists.
Subtasks
- Subtask 1 (30 points): ; for all ; .
- Subtask 2 (30 points): ; for all ; .
- Subtask 3 (40 points): ; for all ; .
Example 1
Input
4 2
1 -1 1 1
Output
4
Explanation
The entire array sums to 2, so [1,4] qualifies and no longer interval exists.
Example 2
Input
3 0
1 2 3
Output
0
Explanation
Every nonempty interval has positive sum, so none sums to zero.