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:
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:
arr = [1, 2, 3, 4, 2, 1]
We can reduce the array to:
[1, 2, 3, 2, 1, 0]
The reductions are:
4 → 2 : cost 2
2 → 1 : cost 1
1 → 0 : cost 1
Total cost:
2 + 1 + 1 = 4
Therefore, the answer is:
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:
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:
left[i] = min(arr[i], left[i - 1] + 1)
For example:
arr = [1, 2, 3, 4, 2, 1]
The left values are:
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:
right[i] = min(arr[i], right[i + 1] + 1)
For the same example:
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:
peak = min(left[i], right[i])
For:
arr = [1, 2, 3, 4, 2, 1]
we get:
left = [1, 2, 3, 4, 2, 1]
right = [1, 2, 3, 3, 2, 1]
So:
peak = [1, 2, 3, 3, 2, 1]
The maximum peak is:
3
Thus, the largest pyramid is:
1 2 3 2 1
Important Mathematical Observation
For a pyramid whose peak is x:
1 2 3 ... x-1 x x-1 ... 3 2 1
The sum of all its elements is:
(1 + 2 + ... + x-1) + x + (x-1 + ... + 2 + 1)
This simplifies to:
x²
For example, if:
x = 3
the pyramid is:
1 2 3 2 1
Its sum is:
1 + 2 + 3 + 2 + 1 = 9
And:
3² = 9
Therefore, once we know the maximum peak height, we can calculate the maximum amount of height that can be retained as:
peak * peak
Java Implementation
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);
}
}

Join the conversation! Your thoughts help the community grow.