|
| 1 | +# 1846. Maximum Element After Decreasing and Rearranging |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Greedy, Sorting. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given an array of positive integers `arr`. Perform some operations (possibly none) on `arr` so that it satisfies these conditions: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- The value of the **first** element in `arr` must be `1`. |
| 14 | + |
| 15 | +- The absolute difference between any 2 adjacent elements must be **less than or equal to **`1`. In other words, `abs(arr[i] - arr[i - 1]) <= 1` for each `i` where `1 <= i < arr.length` (**0-indexed**). `abs(x)` is the absolute value of `x`. |
| 16 | + |
| 17 | + |
| 18 | +There are 2 types of operations that you can perform any number of times: |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +- **Decrease** the value of any element of `arr` to a **smaller positive integer**. |
| 23 | + |
| 24 | +- **Rearrange** the elements of `arr` to be in any order. |
| 25 | + |
| 26 | + |
| 27 | +Return **the **maximum** possible value of an element in **`arr`** after performing the operations to satisfy the conditions**. |
| 28 | + |
| 29 | + |
| 30 | +Example 1: |
| 31 | + |
| 32 | +``` |
| 33 | +Input: arr = [2,2,1,2,1] |
| 34 | +Output: 2 |
| 35 | +Explanation: |
| 36 | +We can satisfy the conditions by rearranging arr so it becomes [1,2,2,2,1]. |
| 37 | +The largest element in arr is 2. |
| 38 | +``` |
| 39 | + |
| 40 | +Example 2: |
| 41 | + |
| 42 | +``` |
| 43 | +Input: arr = [100,1,1000] |
| 44 | +Output: 3 |
| 45 | +Explanation: |
| 46 | +One possible way to satisfy the conditions is by doing the following: |
| 47 | +1. Rearrange arr so it becomes [1,100,1000]. |
| 48 | +2. Decrease the value of the second element to 2. |
| 49 | +3. Decrease the value of the third element to 3. |
| 50 | +Now arr = [1,2,3], which satisfies the conditions. |
| 51 | +The largest element in arr is 3. |
| 52 | +``` |
| 53 | + |
| 54 | +Example 3: |
| 55 | + |
| 56 | +``` |
| 57 | +Input: arr = [1,2,3,4,5] |
| 58 | +Output: 5 |
| 59 | +Explanation: The array already satisfies the conditions, and the largest element is 5. |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +**Constraints:** |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +- `1 <= arr.length <= 105` |
| 68 | + |
| 69 | +- `1 <= arr[i] <= 109` |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +## Solution |
| 74 | + |
| 75 | +```javascript |
| 76 | +/** |
| 77 | + * @param {number[]} arr |
| 78 | + * @return {number} |
| 79 | + */ |
| 80 | +var maximumElementAfterDecrementingAndRearranging = function(arr) { |
| 81 | + arr.sort((a, b) => a - b); |
| 82 | + arr[0] = 1; |
| 83 | + for (var i = 1 ; i < arr.length; i++) { |
| 84 | + if (arr[i] - arr[i - 1] <= 1) continue; |
| 85 | + arr[i] = arr[i - 1] + 1; |
| 86 | + } |
| 87 | + return arr[arr.length - 1]; |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +**Explain:** |
| 92 | + |
| 93 | +nope. |
| 94 | + |
| 95 | +**Complexity:** |
| 96 | + |
| 97 | +* Time complexity : O(n * log(n)). |
| 98 | +* Space complexity : O(1). |
0 commit comments