Subsequence with the Largest Maximum-Minimum Difference

Interview

Time limit0.5sMemory limit256 MB

Summary
Given a sequence, find the shortest contiguous segment whose max minus min equals the largest such difference over all segments.
Level

Medium5 of 10

Topics
Two pointers, Array, Greedy, Implementation
Solved
No attempts yet

Problem

A subsequence of a sequence is a sequence obtained by taking some consecutive terms of the sequence and listing them in their original order. (This is not the usual definition of a subsequence.) Given a sequence AA of length NN, find the length of the shortest subsequence of AA whose value of (maximum in the subsequence) - (minimum in the subsequence) is the largest possible.

Input

The first line gives NN. (1≤N≤1051 \le N \le 10^5)

The next line gives NN numbers separated by spaces, where the ii-th number is AiA_i. (1≤Ai≤1051 \le A_i \le 10^5)

Output

Print a single integer, the answer to the problem.

Examples2

  1. Example 1

    Input
    5
    1 1 1 1 1
    
    Expected output
    1
  2. Example 2

    Input
    5
    1 1 2 3 3
    
    Expected output
    3