Minimum no. of operations required to make an array palindrome
Pattern: Two pointers
Compare the two ends. If they are equal, move both inwards. Otherwise merge the smaller end into its neighbour, counting one operation.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
You are given an array of positive whole numbers. One operation merges two neighbouring values into one value equal to
their sum (the array gets one shorter). Return the fewest operations needed to make the array read the same forwards and
backwards. For example, [1, 4, 5, 1] needs 1 operation (merge 4 and 5 into 9), and [11, 14, 15, 99] needs 3.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
minMerges is this guide's own.
Think first
- Look at the two end values. If they are equal, do they ever need to change?
- If the left end is smaller than the right end, can the left end ever equal the right end without merging?
- Which merge brings the smaller end closer to the bigger one?
Hints
Brute force, and why it fails
Try every sequence of merges and keep the shortest that gives a palindrome. That is exponential, and this guide uses it only to check its tests on small arrays.
The key insight
The two ends must end up equal. The smaller end has no choice but to absorb its neighbour, so the greedy choice is forced, and two pointers apply it in one pass.
Dry run
| merges | 0 |
|---|
arr = [1, 4, 5, 1]. The two ends 1 and 1 are equal, so they already match. Move both inwards.
The plan
i = 0, j = n - 1, merges = 0
while i < j:
if a[i] == a[j]:
i = i + 1; j = j - 1
else if a[i] < a[j]:
i = i + 1; a[i] = a[i] + a[i - 1]; merges = merges + 1
else:
j = j - 1; a[j] = a[j] + a[j + 1]; merges = merges + 1
return merges
Complexity
- Time
O(n)- Space
O(n)
Each step moves a pointer inwards, so there are at most n steps. The guide works on a copy of the values (in 64 bits,
because merged values grow), which is O(n) memory. Changing the input in place would make it O(1).
Edge cases
- Already a palindrome, as in
[15, 4, 15]: 0 operations. - One value: 0.
[1, 2]: one merge gives[3], a palindrome.
I've built my logic, show the code
Java
class Solution {
public int minMerges(int[] arr) {
long[] a = new long[arr.length]; // merged values can grow, so keep them in 64 bits
for (int t = 0; t < arr.length; t++) {
a[t] = arr[t];
}
int i = 0;
int j = a.length - 1;
int merges = 0;
while (i < j) {
if (a[i] == a[j]) {
i++; // the two ends match: move both inwards
j--;
} else if (a[i] < a[j]) {
i++;
a[i] += a[i - 1]; // merge the smaller left end into its neighbour
merges++;
} else {
j--;
a[j] += a[j + 1]; // merge the smaller right end into its neighbour
merges++;
}
}
return merges;
}
}C++
class Solution {
public:
int minMerges(vector<int>& arr) {
vector<long long> a(arr.begin(), arr.end()); // merged values can grow, so keep them in 64 bits
int i = 0;
int j = static_cast<int>(a.size()) - 1;
int merges = 0;
while (i < j) {
if (a[i] == a[j]) {
i++; // the two ends match: move both inwards
j--;
} else if (a[i] < a[j]) {
i++;
a[i] += a[i - 1]; // merge the smaller left end into its neighbour
merges++;
} else {
j--;
a[j] += a[j + 1]; // merge the smaller right end into its neighbour
merges++;
}
}
return merges;
}
};Python
class Solution:
def minMerges(self, arr):
a = list(arr)
i, j = 0, len(a) - 1
merges = 0
while i < j:
if a[i] == a[j]:
i += 1 # the two ends match: move both inwards
j -= 1
elif a[i] < a[j]:
i += 1
a[i] += a[i - 1] # merge the smaller left end into its neighbour
merges += 1
else:
j -= 1
a[j] += a[j + 1] # merge the smaller right end into its neighbour
merges += 1
return mergesCommon mistakes
- Merging the bigger end instead of the smaller one.
- Forgetting to add the merged value into the neighbour, so later comparisons use stale numbers.
- Merged sums can grow large, so a 32-bit type can overflow on big inputs.