Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Arrays

Longest Mountain in an Array

Medium · Arrays · Asked at Google, Amazon, Microsoft

A mountain is a contiguous subarray of length at least 3 that strictly increases to a peak and then strictly decreases. Print the length of the longest mountain, or 0 if there is none.

Input

First line: n. Second line: n space-separated integers.

Output

The length of the longest mountain.

Constraints

One pass: from each possible start, climb while values increase, then descend while they decrease. If both parts are non-empty, it is a mountain. Continue from where the descent ended — every element is visited a constant number of times.

Example 1

Input

7
2 1 4 7 3 2 5

Output

5

Example 2

Input

3
2 2 2

Output

0

Hints

  1. A mountain has a strictly increasing part followed by a strictly decreasing part, both non-empty.
  2. From a starting point, climb as far as possible, then descend as far as possible.
  3. Restart from where the descent ended — mountains can share only their end/start point.

Approach

Let i be a candidate base. Climb j while a[j + 1] > a[j]; remember the peak. Then descend while a[j + 1] < a[j]. If the climb and the descent were both non-empty, j − i + 1 is a mountain. Move i to j (the end of the descent, which can start the next mountain) if anything moved, otherwise to i + 1 (equal neighbours). Each index is passed a constant number of times.

Complexity: O(n) time · O(1) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &x : a) cin >> x;
    int best = 0, i = 0;
    while (i < n) {
        int j = i;
        while (j + 1 < n && a[j + 1] > a[j]) j++;
        int peak = j;
        while (j + 1 < n && a[j + 1] < a[j]) j++;
        if (peak > i && j > peak) best = max(best, j - i + 1);
        i = (j > i) ? j : i + 1;
    }
    cout << best << "\n";
    return 0;
}

Python solution

import sys

data = list(map(int, sys.stdin.read().split()))
n = data[0]
a = data[1 : 1 + n]
best = i = 0
while i < n:
    j = i
    while j + 1 < n and a[j + 1] > a[j]:
        j += 1
    peak = j
    while j + 1 < n and a[j + 1] < a[j]:
        j += 1
    if peak > i and j > peak:
        best = max(best, j - i + 1)
    i = j if j > i else i + 1
print(best)

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval;
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            in.nextToken();
            a[i] = (int) in.nval;
        }
        int best = 0, i = 0;
        while (i < n) {
            int j = i;
            while (j + 1 < n && a[j + 1] > a[j]) j++;
            int peak = j;
            while (j + 1 < n && a[j + 1] < a[j]) j++;
            if (peak > i && j > peak) best = Math.max(best, j - i + 1);
            i = (j > i) ? j : i + 1;
        }
        System.out.println(best);
    }
}

Solve Longest Mountain in an Array online · All practice problems