#K94597. Longest Bitonic Subarray

    ID: 38676 Type: Default 1000ms 256MiB

Longest Bitonic Subarray

Longest Bitonic Subarray

You are given an array of positive integers. A bitonic subarray is a contiguous portion of the array that first increases strictly and then decreases strictly. Note that the increasing part and the decreasing part must each contain at least one element (i.e. the subarray must change direction exactly once), and no adjacent elements in either segment are allowed to be equal.

Your task is to find the length of the longest bitonic subarray. Formally, if you denote by inc[i] the length of the longest strictly increasing subarray ending at index i and by dec[i] the length of the longest strictly decreasing subarray starting at index i, then the length of a bitonic subarray peaking at index i is given by:

\(\text{length} = inc[i] + dec[i] - 1\)

If no valid bitonic subarray exists, output 0.

inputFormat

The input is read from standard input. The first line contains an integer n representing the number of elements in the array. The second line contains n positive integers separated by spaces.

outputFormat

Output a single integer representing the length of the longest bitonic subarray. If no such subarray exists, print 0.

## sample
7
1 2 3 5 4 2 1
7

</p>