LeetCode Problem

How to Solve Maximum Element After Decreasing and Rearranging

To solve this problem, first sort the array and enforce that each element is at most one greater than the previous, applying a greedy choice. By iterating through the sorted array and adjusting values to maintain the invariant, we ensure the maximum element is minimized correctly. This guarantees the largest value after allowed operations is the smallest achievable.

GhostInterview Help

Need help with Maximum Element After Decreasing and Rearranging without spending extra time grinding it?

GhostInterview can read Maximum Element After Decreasing and Rearranging from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1846Greedy choice plus invariant validationReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Greedy choice plus invariant validation
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, first sort the array and enforce that each element is at most one greater than the previous, applying a greedy choice. By iterating through the sorted array and adjusting values to maintain the invariant, we ensure the maximum element is minimized correctly. This guarantees the largest value after allowed operations is the smallest achievable.

Problem Statement

Given an array of positive integers, you may perform operations to decrease elements or rearrange them so that the array satisfies specific adjacency constraints. Each element can be reduced any number of times, and the goal is to adjust the array to meet the rules while keeping all values positive.

You must return the maximum element possible in the array after performing these operations optimally. The challenge is to determine how to rearrange and decrease values using a greedy approach to maintain the invariant that each element is at most one greater than its predecessor.

Examples

Example 1

Input: arr = [2,2,1,2,1]

Output: 2

We can satisfy the conditions by rearranging arr so it becomes [1,2,2,2,1]. The largest element in arr is 2.

Example 2

Input: arr = [100,1,1000]

Output: 3

One possible way to satisfy the conditions is by doing the following:

  1. Rearrange arr so it becomes [1,100,1000].
  2. Decrease the value of the second element to 2.
  3. Decrease the value of the third element to 3. Now arr = [1,2,3], which satisfies the conditions. The largest element in arr is 3.

Example 3

Input: arr = [1,2,3,4,5]

Output: 5

The array already satisfies the conditions, and the largest element is 5.

Constraints

  • 1 <= arr.length <= 105
  • 1 <= arr[i] <= 109

Solution Approach

Sort and Initialize

Start by sorting the array to simplify the greedy adjustments. Initialize the first element as 1 since the smallest value must remain positive.

Iterate with Greedy Adjustment

Traverse the sorted array, setting each element to the minimum of its current value or the previous element plus one. This preserves the invariant while minimizing the maximum element.

Return Maximum Value

After processing all elements, the last element in the adjusted array represents the largest possible value. Return this value as the result.

Complexity Analysis

MetricValue
TimeO(n)
SpaceO(n)

Sorting takes O(n log n) but can be simplified to O(n) with counting sort if values are bounded. Iterating and adjusting is O(n). The space complexity is O(n) if a separate array is used for adjustment, or O(1) in-place.

What Interviewers Usually Probe

  • Look for sorting the array before applying operations.
  • Check if each element can be at most one greater than its previous.
  • Validate the maximum value after greedy adjustments.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to sort before applying greedy decreases.
  • Not enforcing the invariant that arr[i] <= arr[i-1] + 1.
  • Returning the initial max instead of the adjusted maximum element.

Follow-up variants

  • Find maximum after only decreasing elements without rearranging.
  • Return the array configuration instead of just the maximum element.
  • Apply the same approach for arrays with negative numbers allowed.

How GhostInterview Helps

  • GhostInterview provides step-by-step reasoning to apply the greedy invariant pattern correctly.
  • It simulates element adjustments to quickly find the maximum after rearrangements.
  • The solver highlights failure modes if the invariant is violated and guides proper sorting.

Topic Pages

FAQ

What is the main pattern used in Maximum Element After Decreasing and Rearranging?

The key pattern is greedy choice plus invariant validation, ensuring each element is at most one greater than its predecessor after sorting.

Can we avoid sorting the array?

Sorting is necessary to enforce the greedy invariant efficiently; without sorting, determining the correct maximum becomes complex.

What is the time complexity of the optimal solution?

The solution runs in O(n) for the adjustment step and O(n log n) if standard sorting is used, with O(n) space for storing adjusted values.

Does rearranging affect the final maximum value?

Yes, rearranging allows the elements to be positioned optimally so that the invariant holds and the maximum element is minimized.

What are common mistakes in this problem?

Common mistakes include not sorting, failing to enforce the invariant, and returning the original maximum instead of the adjusted maximum value.

GhostInterview Solver

Need direct help with Maximum Element After Decreasing and Rearranging instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximum Element After Decreasing and Rearranging from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.