Roses in a shop
Practice
3.5 (58 votes)
Algorithms
Dynamic programming
Problem
91% Success 10479 Attempts 20 Points 2s Time Limit 256MB Memory 1024 KB Max Code

There are \(n\) roses in a shop. Each rose is assigned an ID. These roses are arranged in order \(1,\ 2,\ 3,\ ... ,\ n\). Each rose has a smell factor denoted as \(smell[i]\) (\(1 \le i \le n \)). You want to buy roses from this shop under the condition that you can buy roses in a segment. In other words, you can purchase roses from \(l\) to \(r\) (\(1 \le l \le r \le n \)). You can remove at most one rose from this segment of roses. Thus, the final length of roses is \(n-1\) or \(n\).

Your task is to calculate the maximum possible length of the strictly-increasing contiguous subarray of the smell factors of these roses.

Note: A contiguous subarray \(a\) with indices from \(l\) to \(r\) is \(a[l,\ ...,\ r]=a[l],\ a[l+1] ,\ ...,\ a[r]\). The subarray \(a[l,\ ...,\ r]\) is strictly increasing if \(a[l]<a[l+1]<\ ...\ <a[r]\).

Input format

  • The first line contains a single integer \(n\) denoting the number of roses that are available in the shop.
  • The second line contains \(n\) space-separated integers \(smell[1],\ smell[2],\ ... ,\ smell[n]\).

Output format

Print one integer denoting the maximum possible length of the strictly-increasing contiguous subarray of the smell factor after removing at most one element.

Constraints

\(2 \le n \le 2\times 10^5 \\ 1 \le smell[i]  \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
1040 votes
Tags:
Ad-HocEasyImplementation
Points:20
703 votes
Tags:
ApprovedData StructuresDynamic ProgrammingEasyNumber TheoryReady
Points:20
22 votes
Tags:
AlgorithmsDynamic ProgrammingEasy