This page is still under construction.

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

Islands

Interview

Time limit1sMemory limit128 MB

Summary
Given heights along a line, find the maximum number of separate exposed segments at any single rising water level.
Level

Medium5 of 10

Topics
Sorting, Array, Simulation, Implementation
Solved
No attempts yet

Problem

Whenever it rains, Farmer John's field floods. Because the field is not perfectly level, the water rises unevenly, and the exposed land breaks up into a number of disjoint "islands".

The field is a one-dimensional landscape given by NN consecutive height values H1,H2,…,HNH_1, H_2, \dots, H_N. Assume the field is bounded on both ends by walls of effectively infinite height. As a rainstorm fills the field, the lowest regions are covered first, creating a number of separate islands, and eventually everything is submerged. The instant the water level becomes equal to the height of a piece of land, that piece of land is considered underwater.

For example, with the heights 3,5,2,3,1,4,2,33, 5, 2, 3, 1, 4, 2, 3, adding just over 11 unit of water leaves 44 islands (the most that ever appear at once); after a total of 77 units of water only 22 islands remain exposed.

Compute the maximum number of islands that are ever visible at a single point in time, as the water rises from empty until the entire field is underwater.

Input

  • The first line contains the integer NN (1≤N≤100,0001 \le N \le 100{,}000).
  • Each of the next NN lines contains one height HiH_i (1≤Hi≤1,000,000,0001 \le H_i \le 1{,}000{,}000{,}000).

Output

  • Print a single integer: the maximum number of islands that appear at any single moment during the storm.

Hint

Consider the heights 3,5,2,3,1,4,2,33, 5, 2, 3, 1, 4, 2, 3. When the water level lies strictly between 22 and 33, only the cells of height 33 or more remain above water, splitting the land into 44 separate islands — the maximum seen at any single instant.

Examples2

  1. Example 1

    Input
    8
    3
    5
    2
    3
    1
    4
    2
    3
    
    Expected output
    4
    
  2. Example 2

    Input
    5
    1
    5
    1
    5
    1
    
    Expected output
    2