Find minimum number of merge operations to make an array palindrome
Given an array of positive integers. We need to make the given array a ‘Palindrome’. The only allowed operation is”merging” (of two adjacent elements). Merging two adjacent elements means replacing them with their sum. The task is to find the minimum number of merge operations required to make the given array a ‘Palindrome’.
To make any array a palindrome, we can simply apply merge operation n-1 times where n is the size of the array (because a single-element array is always palindromic, similar to single-character string). In that case, the size of array will be reduced to 1. But in this problem, we are asked to do it in the minimum number of operations.
Example :
Input : arr[] = {15, 4, 15}
Output : 0
Array is already a palindrome. So we
do not need any merge operation.
Input : arr[] = {1, 4, 5, 1}
Output : 1
We can make given array palindrome with
minimum one merging (merging 4 and 5 to
make 9)
Input : arr[] = {11, 14, 15, 99}
Output : 3
We need to merge all elements to make
a palindrome.
The expected time complexity is O(n).
Let f(i, j) be minimum merging operations to make subarray arr[i..j] a palindrome. If i == j answer is 0. We start i from 0 and j from n-1.
- If arr[i] == arr[j], then there is no need to do any merging operations at index i or index j. Our answer in this case will be f(i+1, j-1).
- Else, we need to do merging operations. Following cases arise.
- If arr[i] > arr[j], then we should do merging operation at index j. We merge index j-1 and j, and update arr[j-1] = arr[j-1] + arr[j]. Our answer in this case will be 1 + f(i, j-1).
- For the case when arr[i] < arr[j], update arr[i+1] = arr[i+1] + arr[i]. Our answer in this case will be 1 + f(i+1, j).
- Our answer will be f(0, n-1), where n is the size of array arr[].
Therefore this problem can be solved iteratively using two pointers (first pointer pointing to start of the array and second pointer pointing to the last element of the array) method and keeping count of total merging operations done till now.
Below is an implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int findMinOps( int arr[], int n)
{
int ans = 0;
for ( int i=0,j=n-1; i<=j;)
{
if (arr[i] == arr[j])
{
i++;
j--;
}
else if (arr[i] > arr[j])
{
j--;
arr[j] += arr[j+1] ;
ans++;
}
else
{
i++;
arr[i] += arr[i-1];
ans++;
}
}
return ans;
}
int main()
{
int arr[] = {1, 4, 5, 9, 1};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << "Count of minimum operations is "
<< findMinOps(arr, n) << endl;
return 0;
}
|
Java
class GFG
{
static int findMinOps( int [] arr, int n)
{
int ans = 0 ;
for ( int i= 0 ,j=n- 1 ; i<=j;)
{
if (arr[i] == arr[j])
{
i++;
j--;
}
else if (arr[i] > arr[j])
{
j--;
arr[j] += arr[j+ 1 ] ;
ans++;
}
else
{
i++;
arr[i] += arr[i- 1 ];
ans++;
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = new int []{ 1 , 4 , 5 , 9 , 1 } ;
System.out.println( "Count of minimum operations is " +
findMinOps(arr, arr.length));
}
}
|
Python3
def findMinOps(arr, n):
ans = 0
i,j = 0 ,n - 1
while i< = j:
if arr[i] = = arr[j]:
i + = 1
j - = 1
elif arr[i] > arr[j]:
j - = 1
arr[j] + = arr[j + 1 ]
ans + = 1
else :
i + = 1
arr[i] + = arr[i - 1 ]
ans + = 1
return ans
arr = [ 1 , 4 , 5 , 9 , 1 ]
n = len (arr)
print ( "Count of minimum operations is " + str (findMinOps(arr, n)))
|
C#
using System;
class GFG
{
static int findMinOps( int []arr, int n)
{
int ans = 0;
for ( int i = 0, j = n - 1; i <= j;)
{
if (arr[i] == arr[j])
{
i++;
j--;
}
else if (arr[i] > arr[j])
{
j--;
arr[j] += arr[j + 1] ;
ans++;
}
else
{
i++;
arr[i] += arr[i-1];
ans++;
}
}
return ans;
}
public static void Main()
{
int []arr = new int []{1, 4, 5, 9, 1} ;
Console.Write( "Count of minimum operations is " +
findMinOps(arr, arr.Length));
}
}
|
Javascript
<script>
function findMinOps(arr, n)
{
let ans = 0;
for (let i=0,j=n-1; i<=j;)
{
if (arr[i] == arr[j])
{
i++;
j--;
}
else if (arr[i] > arr[j])
{
j--;
arr[j] += arr[j+1] ;
ans++;
}
else
{
i++;
arr[i] += arr[i-1];
ans++;
}
}
return ans;
}
let arr = [1, 4, 5, 9, 1];
document.write( "Count of minimum operations is " +
findMinOps(arr, arr.length));
</script>
|
PHP
<?php
function findMinOps( $arr , $n )
{
$ans = 1;
for ( $i = 0, $j = $n - 1; $i <= $j 😉
{
if ( $arr [ $i ] == $arr [ $j ])
{
$i ++;
$j --;
}
else if ( $arr [ $i ] > $arr [ $j ])
{
$j --;
$arr [ $j ] += $arr [ $j + 1] ;
$ans ++;
}
else
{
$i ++;
$arr [ $i ] += $arr [ $i - 1];
$ans ++;
}
}
return $ans ;
}
$arr [] = array (1, 4, 5, 9, 1);
$n = sizeof( $arr );
echo "Count of minimum operations is " ,
findMinOps( $arr , $n ) ;
?>
|
Output
Count of minimum operations is 1
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach#2: Using dynamic programming
Create a 2D array dp of size n x n where n is the length of the input array arr. Initialize all elements of dp to 0. For each gap value from 1 to n – 1, loop over all i values from 0 to n – gap – 1. Compute the corresponding j value as i + gap. If arr[i] is equal to arr[j], then set dp[i][j] to dp[i + 1][j – 1].Otherwise, set dp[i][j] to min(dp[i][j – 1], dp[i + 1][j]) + 1. The minimum number of merge operations required to make arr a palindrome is dp[0][n – 1].
Algorithm
1. Initialize count variable to 0.
2. Initialize two pointers i and j to the start and end of the array respectively.
3. While i is less than j,
a. If arr[i] is equal to arr[j], increment i and decrement j.
b. Else, if arr[i] is less than arr[j], increment i by 1 and add arr[i-1] to arr[i]. Increment count by 1.
c. Else, decrement j by 1 and add arr[j+1] to arr[j]. Increment count by 1.
4. Return count.
C++
#include <iostream>
#include <vector>
using namespace std;
int minMergeOperations(vector< int > arr)
{
int n = arr.size();
vector<vector< int > > dp(n, vector< int >(n, 0));
for ( int gap = 1; gap < n; gap++) {
for ( int i = 0; i < n - gap; i++) {
int j = i + gap;
if (arr[i] == arr[j]) {
dp[i][j] = dp[i + 1][j - 1];
}
else {
dp[i][j]
= min(dp[i][j - 1], dp[i + 1][j]) + 1;
}
}
}
return dp[0][n - 1];
}
int main()
{
vector< int > arr = { 11, 14, 15, 99 };
cout << minMergeOperations(arr) << endl;
return 0;
}
|
Java
import java.util.Arrays;
import java.util.Vector;
public class MinMergeOperations {
static int minMergeOperations(Vector<Integer> arr) {
int n = arr.size();
int [][] dp = new int [n][n];
for ( int gap = 1 ; gap < n; gap++) {
for ( int i = 0 ; i < n - gap; i++) {
int j = i + gap;
if (arr.get(i).equals(arr.get(j))) {
dp[i][j] = dp[i + 1 ][j - 1 ];
}
else {
dp[i][j] = Math.min(dp[i][j - 1 ], dp[i + 1 ][j]) + 1 ;
}
}
}
return dp[ 0 ][n - 1 ];
}
public static void main(String[] args) {
Vector<Integer> arr = new Vector<>(Arrays.asList( 11 , 14 , 15 , 99 ));
System.out.println(minMergeOperations(arr));
}
}
|
Python3
def min_merge_operations(arr):
n = len (arr)
dp = [[ 0 for j in range (n)] for i in range (n)]
for gap in range ( 1 , n):
for i in range (n - gap):
j = i + gap
if arr[i] = = arr[j]:
dp[i][j] = dp[i + 1 ][j - 1 ]
else :
dp[i][j] = min (dp[i][j - 1 ], dp[i + 1 ][j]) + 1
return dp[ 0 ][n - 1 ]
arr = [ 11 , 14 , 15 , 99 ]
print (min_merge_operations(arr))
|
C#
using System;
using System.Collections.Generic;
class Program {
static int MinMergeOperations(List< int > arr)
{
int n = arr.Count;
int [][] dp = new int [n][];
for ( int i = 0; i < n; i++) {
dp[i] = new int [n];
}
for ( int gap = 1; gap < n; gap++) {
for ( int i = 0; i < n - gap; i++) {
int j = i + gap;
if (arr[i] == arr[j]) {
dp[i][j] = dp[i + 1][j - 1];
}
else {
dp[i][j] = Math.Min(dp[i][j - 1],
dp[i + 1][j])
+ 1;
}
}
}
return dp[0][n - 1];
}
static void Main()
{
List< int > arr = new List< int >{ 11, 14, 15, 99 };
int minMergeOps = MinMergeOperations(arr);
Console.WriteLine( "Minimum merge operations: "
+ minMergeOps);
}
}
|
Javascript
function min_merge_operations(arr) {
let n = arr.length;
let dp = Array.from(Array(n), () => Array(n).fill(0));
for (let gap = 1; gap < n; gap++) {
for (let i = 0; i < n - gap; i++) {
let j = i + gap;
if (arr[i] == arr[j]) {
dp[i][j] = dp[i + 1][j - 1];
} else {
dp[i][j] = Math.min(dp[i][j - 1], dp[i + 1][j]) + 1;
}
}
}
return dp[0][n - 1];
}
let arr = [11, 14, 15, 99];
console.log(min_merge_operations(arr));
|
Time Complexity: O(n), where n is length of array
Auxiliary Space: O(1)
Last Updated :
04 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...