Problem Statement

Given an array arr[] where arr[i] represents the height of the i-th stone, we need to convert the array into a pyramid by only reducing the heights of the stones.

Reducing the height of a stone by 1 costs 1 unit.

A valid pyramid has the following form:

Code
1 2 3 ... x-1 x x-1 ... 3 2 1

The pyramid must be a contiguous subarray, and every element outside the pyramid must become 0.

The goal is to find the minimum total reduction cost required to form a valid pyramid.


Example

Consider:

Code
arr = [1, 2, 3, 4, 2, 1]

We can reduce the array to:

Code
[1, 2, 3, 2, 1, 0]

The reductions are:

Code
4 → 2 : cost 2
2 → 1 : cost 1
1 → 0 : cost 1

Total cost:

Code
2 + 1 + 1 = 4

Therefore, the answer is:

Code
4

Key Idea

Since we can only reduce the heights, we should try to keep as much of the original array as possible while forming a valid pyramid.

Therefore:

Code
Minimum Cost
= Total Height of Original Array
  - Maximum Height That Can Be Kept as a Pyramid

So the main problem becomes:

Find the maximum possible pyramid that can be formed from the given array.


Dynamic Programming Approach

For every position i, we calculate two values.

1. left[i]

left[i] represents the maximum possible height at position i when we build the pyramid from the left side.

The height can increase by at most 1 from the previous position.

Therefore:

Code
left[i] = min(arr[i], left[i - 1] + 1)

For example:

Code
arr = [1, 2, 3, 4, 2, 1]

The left values are:

Code
left = [1, 2, 3, 4, 2, 1]

2. right[i]

Similarly, right[i] represents the maximum possible height at position i when we build from the right side.

The recurrence is:

Code
right[i] = min(arr[i], right[i + 1] + 1)

For the same example:

Code
right = [1, 2, 3, 3, 2, 1]

Finding the Peak

For a position i to be the peak of the pyramid, it must satisfy both the left and right constraints.

Therefore, the maximum possible peak height at i is:

Code
peak = min(left[i], right[i])

For:

Code
arr = [1, 2, 3, 4, 2, 1]

we get:

Code
left  = [1, 2, 3, 4, 2, 1]
right = [1, 2, 3, 3, 2, 1]

So:

Code
peak = [1, 2, 3, 3, 2, 1]

The maximum peak is:

Code
3

Thus, the largest pyramid is:

Code
1 2 3 2 1

Important Mathematical Observation

For a pyramid whose peak is x:

Code
1 2 3 ... x-1 x x-1 ... 3 2 1

The sum of all its elements is:

Code
(1 + 2 + ... + x-1) + x + (x-1 + ... + 2 + 1)

This simplifies to:

Code
x²

For example, if:

Code
x = 3

the pyramid is:

Code
1 2 3 2 1

Its sum is:

Code
1 + 2 + 3 + 2 + 1 = 9

And:

Code
3² = 9

Therefore, once we know the maximum peak height, we can calculate the maximum amount of height that can be retained as:

Code
peak * peak

Java Implementation
Java
class Solution {
    public int formPyramid(int[] arr) {
        int n = arr.length;

        int[] left = new int[n];
        int[] right = new int[n];

        // Calculate maximum possible height from the left
        left[0] = 1;

        for (int i = 1; i < n; i++) {
            left[i] = Math.min(arr[i], left[i - 1] + 1);
        }

        // Calculate maximum possible height from the right
        right[n - 1] = 1;

        for (int i = n - 2; i >= 0; i--) {
            right[i] = Math.min(arr[i], right[i + 1] + 1);
        }

        // Find the maximum pyramid that can be formed
        long maxPyramidSum = 0;

        for (int i = 0; i < n; i++) {
            int peak = Math.min(left[i], right[i]);

            long pyramidSum = (long) peak * peak;

            maxPyramidSum = Math.max(maxPyramidSum, pyramidSum);
        }

        // Calculate total height of the original array
        long totalSum = 0;

        for (int height : arr) {
            totalSum += height;
        }

        // Required reduction cost
        return (int) (totalSum - maxPyramidSum);
    }
}

Code Explanation

Step 1: Create DP Arrays

Java
int[] left = new int[n];
int[] right = new int[n];

We use two arrays:

  • left[] → constraints from the left

  • right[] → constraints from the right


Step 2: Calculate left[]

Java
left[0] = 1;

for (int i = 1; i < n; i++) {
    left[i] = Math.min(arr[i], left[i - 1] + 1);
}

At every position, the height cannot be greater than:

Code
previous height + 1

and it also cannot be greater than the original stone height.

Hence:

Code
left[i] = min(arr[i], left[i - 1] + 1)

Step 3: Calculate right[]

Java
right[n - 1] = 1;

for (int i = n - 2; i >= 0; i--) {
    right[i] = Math.min(arr[i], right[i + 1] + 1);
}

This performs the same calculation from the right side.

It ensures that the pyramid can decrease by exactly 1 as we move away from the peak.


Step 4: Find the Maximum Peak

Java
int peak = Math.min(left[i], right[i]);

A position can be a pyramid peak only if both sides can support that height.

Therefore, we take the smaller value.

For example:

Code
left[i]  = 4
right[i] = 3

The peak can only be:

Code
min(4, 3) = 3

Step 5: Calculate Pyramid Sum

Java
long pyramidSum = (long) peak * peak;

A pyramid with peak x has total height:

Code
x²

So there is no need to explicitly construct the pyramid.


Step 6: Calculate Original Sum

Java
long totalSum = 0;

for (int height : arr) {
    totalSum += height;
}

This gives the total height of all stones before performing any reductions.


Step 7: Calculate Minimum Cost

Java
return (int) (totalSum - maxPyramidSum);

The stones that remain in the final pyramid are not reduced.

Everything else must be removed or reduced.

Therefore:

Code
Minimum Cost
= Original Total Height
  - Maximum Pyramid Height

Dry Run

Consider:

Code
arr = [1, 2, 3, 4, 2, 1]

Left DP

Code
left[0] = 1

left[1] = min(2, 1 + 1) = 2
left[2] = min(3, 2 + 1) = 3
left[3] = min(4, 3 + 1) = 4
left[4] = min(2, 4 + 1) = 2
left[5] = min(1, 2 + 1) = 1

Therefore:

Code
left = [1, 2, 3, 4, 2, 1]

Right DP

Code
right[5] = 1

right[4] = min(2, 1 + 1) = 2
right[3] = min(4, 2 + 1) = 3
right[2] = min(3, 3 + 1) = 3
right[1] = min(2, 3 + 1) = 2
right[0] = min(1, 2 + 1) = 1

Therefore:

Code
right = [1, 2, 3, 3, 2, 1]

Possible Peaks

Code
min(left[i], right[i])

gives:

Code
[1, 2, 3, 3, 2, 1]

Maximum peak:

Code
3

Maximum pyramid sum:

Code
3 × 3 = 9

Original array sum:

Code
1 + 2 + 3 + 4 + 2 + 1 = 13

Therefore:

Code
Minimum Cost = 13 - 9
             = 4

Why Does This Work?

The important observation is that the final pyramid must increase by 1 until the peak and decrease by 1 after the peak.

The left[] array tells us how high the pyramid can grow from the left.

The right[] array tells us how high it can grow from the right.

Taking:

Code
min(left[i], right[i])

ensures that both sides of the pyramid are valid.

Among all possible peaks, choosing the largest peak retains the maximum total stone height. Since the only operation allowed is reduction, retaining the maximum possible height gives the minimum reduction cost.


Complexity Analysis

Time Complexity

We perform three linear passes:

Code
O(n) + O(n) + O(n)

Therefore:

Code
O(n)

Auxiliary Space

We use two arrays of size n:

Code
left[n]
right[n]

Therefore:

Code
O(n)

Edge Cases

Already a pyramid

Code
arr = [1, 2, 1]

The maximum pyramid is the entire array.

Code
Original sum = 4
Pyramid sum  = 4
Answer       = 0

Single element

Code
arr = [5]

The valid pyramid can only have height 1:

Code
[1]

Therefore:

Code
Cost = 5 - 1 = 4

Large values

Since arr[i] can be up to 10^5, using long for the sum calculations is safer than using int.


Conclusion

The main trick in this problem is to avoid trying every possible pyramid explicitly.

Using two DP arrays:

Code
left[i]
right[i]

we can determine the maximum possible peak at every position:

Code
peak = min(left[i], right[i])

A pyramid with peak x has a total height of:

Code
x²

Finally:

Code
Minimum Reduction Cost
= Total Array Sum - Maximum Pyramid Sum

This gives an efficient O(n) time and O(n) space solution.