PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000139. Đếm đoạn có tổng bằng K (Subarray Sum Equals 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
Đếm đoạn có tổng bằng K (Subarray Sum Equals K)
Subarray Sum Equals K
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given an integer sequence and a target integer , count all nonempty contiguous intervals such that and . Different pairs of endpoints count as different intervals, even if their elements are identical. The array may contain negative numbers and zeros.
Input
- Line 1: two integers and , the array length and the target sum.
- Line 2: integers in order.
Output
Print one integer: the number of qualifying nonempty intervals. Print 0 if none exists.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Example 1
Input
3 2
1 1 1
Output
2
Explanation
Intervals and both sum to 2. Singletons sum to 1 and the full array sums to 3.
Example 2
Input
3 3
1 2 3
Output
2
Explanation
The intervals and sum to 3; no other interval does.