#105. 数组最大跨度

    ID: 105 Type: Default 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>codingbatArray-3gesp5STL哈希

数组最大跨度

Maximum Span in Array

Background

Congcong is currently studying the properties of arrays. He found that the occurrences of identical elements in an array can sometimes be interesting, so he defined a new concept: "span".

Problem Description

Consider the leftmost and rightmost appearances of some value in an array. We'll say that the "span" is the number of elements between the two inclusive. A single value has a span of 11. Returns the largest span found in the given array. (Efficiency is not a priority.)

Input Format

Input is given from standard input in the following format.

A single line containing several space-separated integers, representing the elements of the array.

Output Format

Output to standard output in the following format.

A single integer, representing the largest span found in the array.

Sample

1 2 1 1 3
4
1 4 2 1 4 1 4
6
1 4 2 1 4 4 4
6

Sample Explanation

Sample 1: For the array [1, 2, 1, 1, 3]:

  • Value 11 appears leftmost at index 00 and rightmost at index 33. The span is 30+1=43 - 0 + 1 = 4.
  • Value 22 appears leftmost at index 11 and rightmost at index 11. The span is 11+1=11 - 1 + 1 = 1.
  • Value 33 appears leftmost at index 44 and rightmost at index 44. The span is 44+1=14 - 4 + 1 = 1. The maximum span is 44.

Sample 2: For the array [1, 4, 2, 1, 4, 1, 4]:

  • Value 11 appears leftmost at index 00 and rightmost at index 55. The span is 50+1=65 - 0 + 1 = 6.
  • Value 44 appears leftmost at index 11 and rightmost at index 66. The span is 61+1=66 - 1 + 1 = 6.
  • Value 22 appears leftmost at index 22 and rightmost at index 22. The span is 22+1=12 - 2 + 1 = 1. The maximum span is 66.

Sample 3: For the array [1, 4, 2, 1, 4, 4, 4]:

  • Value 11 appears leftmost at index 00 and rightmost at index 33. The span is 30+1=43 - 0 + 1 = 4.
  • Value 44 appears leftmost at index 11 and rightmost at index 66. The span is 61+1=66 - 1 + 1 = 6.
  • Value 22 appears leftmost at index 22 and rightmost at index 22. The span is 22+1=12 - 2 + 1 = 1. The maximum span is 66.

Constraints

Time limit: 11 second, Memory limit: 10241024 KiB for each test case.