#K91162. Maximum Sum Contiguous Subarray of Size k
Maximum Sum Contiguous Subarray of Size k
Maximum Sum Contiguous Subarray of Size k
You are given an array of integers and an integer k. Your task is to find the maximum sum of any contiguous subarray of exactly size k.
If the array length is less than k or the array is empty, the answer is 0.
Hint: A sliding window approach can be used to solve this problem in O(n) time. The sum of a subarray starting at index i (for i from 0 to n-k) is given by:
\(S = \sum_{j=i}^{i+k-1} arr[j]\)
Your solution should read from standard input (stdin) and write the answer to standard output (stdout).
inputFormat
The input consists of three lines:
- The first line contains an integer n denoting the number of elements in the array.
- The second line contains n space-separated integers representing the array elements. If n is 0, this line will be empty.
- The third line contains an integer k denoting the size of the contiguous subarray.
outputFormat
Output a single integer which is the maximum sum of any contiguous subarray of size k. If k is greater than n (or the array is empty), output 0.
## sample8
1 3 -1 -3 5 3 6 7
3
16