This page is still under construction.

Parts of this page are still being built. What you see may change.

Pear Trees and Apple Trees

Interview

Time limit1sMemory limit128 MB

Summary
Find the largest distance between any pear tree and any apple tree in a row of n trees.
Level

Easy2 of 10

Topics
Array
Solved
No attempts yet

Problem

In an orchard, pear trees and apple trees are all planted along a straight line, each spaced 11 meter apart from the next. The orchard's owner, Mr. Wiktor, wants to find the pear tree that is farthest from any apple tree. Help him by computing the maximum distance between a pear tree and an apple tree.

Input

The first line contains an integer nn (2≤n≤1062 \le n \le 10^6), the number of trees in the orchard. The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n separated by spaces. Each aia_i gives the type of the ii-th tree: 00 means a pear tree and 11 means an apple tree. You may assume the orchard contains at least one pear tree and at least one apple tree.

Output

Print a single integer: the maximum distance between a pear tree and an apple tree. Because adjacent trees are 11 meter apart, the distance between the ii-th and jj-th trees is ∣i−j∣|i - j| meters.

Examples5

  1. Example 1

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

    Input
    2
    0 1
    
    Expected output
    1
    
  3. Example 3

    Input
    2
    1 0
    
    Expected output
    1
    
  4. Example 4

    Input
    6
    0 0 0 1 1 1
    
    Expected output
    5
    
  5. Example 5

    Input
    8
    0 1 0 1 0 1 0 1
    
    Expected output
    7