#C14023. Longest Increasing Subarray with Fixed Difference

    ID: 43627 Type: Default 1000ms 256MiB

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] and n = 1, the answer is 5.
  • For arr = [4, 4, 4] and n = 1, the answer is 1.
  • For arr = [1, 3, 5, 7] and n = 2, the answer is 4.

inputFormat

The input is provided via stdin and consists of two lines:

  1. The first line contains two integers m and n, where m is the number of elements in the array and n is the required difference between consecutive elements.
  2. The second line contains m space-separated integers representing the array. If m 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.

## sample
0 1
0