Problem 8

Harmonious Interval

Data Structures

Problem Statement

In the kingdom of Melodia, the royal composer seeks the secret to a perfect symphony. Legend has it that a contiguous sequence of notes, when arranged in the correct order, creates an interval of perfect harmony. Specifically, a sequence of frequencies is said to be harmonious if the difference between its maximum and minimum frequency is exactly kk. The composer has been handed a mysterious sequence of frequencies by a wandering minstrel, and he now tasks you with deciphering the length of the longest harmonious interval hidden within.

Your task is to help the composer by determining the length of the longest contiguous subarray of frequencies such that the difference between the maximum and minimum values in the subarray is exactly kk. If no such subarray exists, output 1-1.

Input Format

  • The first line of input contains two space-separated integers:
    • n5000n \leq 5000, the number of frequencies in the sequence, and
    • k100k \leq 100, the required harmonious difference.
  • The second line contains nn space-separated integers representing the sequence of frequencies (all numbers 1500\leq 1500).

Output Format

Print a single integer representing the length of the longest contiguous subarray for which
max(subarray)min(subarray)=k.\text{max(subarray)} - \text{min(subarray)} = k.
If no such subarray exists, output 1-1.

Examples

Input

5 3
1 3 6 4 1

Output

3

Input

4 5
2 2 2 2

Output

-1

Code Editor

Language
Theme