Maximum Element After Decreasing and Rearranging

You are given an array of positive integers `arr`. You may rearrange the elements in any order and decrease any element to a smaller positive integer, as many times as you like. After that the array must satisfy: the first element is `1`, and every two adjacent elements differ by at most `1`. Return the largest possible value of the maximum element.

30:00

Maximum Element After Decreasing and Rearranging
medium
Free written solution
Topics
Companies

You are given an array of positive integers arr. You may rearrange the elements in any order and decrease any element to a smaller positive integer, as many times as you like. After that the array must satisfy: the first element is 1, and every two adjacent elements differ by at most 1. Return the largest possible value of the maximum element.

Example 1:
Input: {"arr":[2,2,1,2,1]}
Output: 2
Constraints:
  • 1≤arr.length≤1051 \leq \text{arr.length} \leq 10^5

  • 1≤arr[i]≤1091 \leq \text{arr}[i] \leq 10^9

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[2,2,1,2,1]}

input arr (any order)

2
2
1
2
1

values as given

2

2

1

2

1

We get 5 values in any order. We may rearrange them and only ever decrease them, and the result must start at 1 and rise by at most 1 per step.

Variables
No variables to display
DepthFunction Call
Stack empty
0/7