PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000143. Đếm đoạn có đúng K số lẻ (Count Number of Nice Subarrays)
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ó đúng K số lẻ (Count Number of Nice Subarrays)
Count Number of Nice Subarrays
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given positive integers and an integer , a nonempty contiguous subarray is nice if it contains exactly odd elements. Count all nice subarrays, distinguishing different endpoint pairs.
Input
- Line 1: integer , the array length and integer , the required number of odd entries.
- Line 2: integers in order.
Output
Print the number of qualifying intervals, or 0 if there are none.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; ; Every is odd.
- Subtask 3 (50 points): ; ; ; .
Example 1
Input
5 3
1 1 2 1 1
Output
2
Explanation
Intervals and contain exactly three odd values. The entire array has four odds; no other interval has three.
Example 2
Input
3 1
2 4 6
Output
0
Explanation
All values are even, so no interval contains one odd value.
Example 3
Input
10 2
2 2 2 1 2 2 1 2 2 2
Output
16
Explanation
The odd entries are at positions 4 and 7. There are four left endpoints (1 through 4) and four right endpoints (7 through 10), giving .