Count Even Numbers in Window
EasyPractice library · +10 XPsliding windowarrays
Given an array of N integers and an integer K, print the count of even numbers in each contiguous subarray (window) of size K. The windows are considered from left to right.
Input
The first line contains two integers N and K, separated by a space. The second line contains N integers representing the array elements.
Output
Print N - K + 1 integers, where each integer is the count of even numbers in a window of size K. The counts should be printed on a single line, separated by spaces.
Constraints
1 <= K <= N <= 1000. Array elements are integers between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 3 1 2 3 4 5
Output
1 2 1
Windows are [1,2,3] (1 even), [2,3,4] (2 evens), [3,4,5] (1 even).
Example 2
Input
7 4 2 4 6 1 3 8 10
Output
3 2 2 2
Windows are [2,4,6,1] (3 evens), [4,6,1,3] (2 evens), [6,1,3,8] (2 evens), [1,3,8,10] (2 evens).
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 3 1 2 3 4 5
Example 2 input
7 4 2 4 6 1 3 8 10