#C14023. Longest Increasing Subarray with Fixed Difference
Longest Increasing Subarray with Fixed Difference
Longest Increasing Subarray with Fixed Difference
Given an array of integers and an integer n, find the length of the longest contiguous subarray where the difference between every two consecutive elements is exactly n.
If the array is empty or contains less than two elements, output 0. Otherwise, even if no two consecutive elements have the required difference, a single element is considered a subarray of length 1.
Formally, for a subarray \(a_1, a_2, \ldots, a_k\) (with \(k \ge 2\)), it must hold that \(a_i - a_{i-1} = n\) for every \(2 \le i \le k\). The goal is to maximize \(k\).
Examples:
- For
arr = [1, 2, 3, 4, 5]
andn = 1
, the answer is5
. - For
arr = [4, 4, 4]
andn = 1
, the answer is1
. - For
arr = [1, 3, 5, 7]
andn = 2
, the answer is4
.
inputFormat
The input is provided via stdin and consists of two lines:
- The first line contains two integers
m
andn
, wherem
is the number of elements in the array andn
is the required difference between consecutive elements. - The second line contains
m
space-separated integers representing the array. Ifm
is 0, this line will be empty.
outputFormat
Output a single integer to stdout representing the length of the longest contiguous subarray that meets the criteria.
## sample0 1
0