#K56837. Count Unique Maximum Sum Subarrays
Count Unique Maximum Sum Subarrays
Count Unique Maximum Sum Subarrays
You are given an array of integers and an integer k. Your task is to determine the number of unique contiguous subarrays of length k that have the maximum sum among all possible contiguous subarrays of length k.
A subarray is a contiguous sequence of elements within the array. Two subarrays are considered unique if their elements (in order) are different. Note that even if multiple subarrays yield the same maximum sum, only one unique subarray is counted if they have identical elements.
Input Format: The input is provided via standard input (stdin). The first line contains space-separated integers representing the array. The second line contains the integer k.
Output Format: Print a single integer to standard output (stdout) which is the count of unique contiguous subarrays of length k that achieve the maximum sum. If no subarray of length k exists (for example, when k is greater than the array's length), output 0.
It can be shown that if a valid subarray exists, the answer according to the test cases provided is always 1
because the maximum sum is unique by value. However, if k is greater than the number of elements in the array or the array is empty, output 0.
Note: Any mathematical formula should be formatted in \(\LaTeX\). For example, the sum of a subarray of length \(k\) starting at index \(i\) can be written as \(S_i = \sum_{j=i}^{i+k-1} a_j\).
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains the elements of the array as space-separated integers.
- The second line contains the integer k.
outputFormat
Output a single integer to stdout which is the count of unique contiguous subarrays of length k that have the maximum sum. If there is no valid subarray, output 0
.
1 2 3 4 5
2
1