#C1033. Maximum Tower Height
Maximum Tower Height
Maximum Tower Height
Problem Description:
You are given n blocks with given heights. You are allowed to modify exactly one block's height (a wildcard modification) in order to form a tower. The tower is constructed by stacking blocks such that for any two adjacent blocks, the absolute difference in their heights is at most 1. Note that you cannot add extra blocks – you must form your tower using the provided blocks (with one allowed modification).
Your task is to determine the maximum possible height (i.e. the number of blocks used) of the tower after applying an optimal modification. The modification is only allowed to adjust a block's height by filling a gap of exactly 1 missing integer between consecutive blocks. In other words, if two consecutive sorted blocks differ by 2, you can modify one of them to bridge the gap (using the wildcard exactly once), but if the gap is larger than 2 then the sequence cannot be connected further. The blocks can be rearranged arbitrarily to achieve the best outcome.
inputFormat
Input Format:
The first line contains a single integer (n), representing the number of blocks.
The second line contains (n) space-separated integers, representing the heights of the blocks.
outputFormat
Output Format:
Output a single integer representing the maximum possible height of the tower (i.e., the maximum number of blocks that can be arranged consecutively after at most one modification).## sample
5
3 7 4 2 5
5