#C9243. Longest Subarray with At Most K Even Numbers
Longest Subarray with At Most K Even Numbers
Longest Subarray with At Most K Even Numbers
You are given an array of integers and an integer k. Your task is to find the length of the longest contiguous subarray that contains at most k even numbers.
An even number is an integer divisible by 2. Formally, given an array \(a_1, a_2, \ldots, a_n\) and an integer \(k\), you need to find the maximum length \(L\) such that there exists an index interval \([i, j]\) (with \(1 \le i \le j \le n\)) where the number of even numbers in \(a_i, a_{i+1}, \ldots, a_j\) is at most \(k\).
This problem can be efficiently solved using a sliding window (two pointers) technique.
inputFormat
The input is read from standard input (stdin) and has the following format:
n k a1 a2 a3 ... an
Where:
- \(n\) is the number of elements in the array.
- \(k\) is an integer representing the maximum allowed even numbers in the subarray.
- The second line has \(n\) space-separated integers representing the array elements.
outputFormat
Output a single integer which is the length of the longest contiguous subarray that contains at most k even numbers. The output should be written to standard output (stdout).
## sample10 2
1 2 3 4 5 6 7 8 9 10
5