Given an array arr[] consisting of N elements, the task is to find the minimum palindromic subarray removals required to remove all elements from the array.
Examples:
Input: arr[] = {1, 3, 4, 1, 5}, N = 5
Output: 3
Explanation:
Removal of 4 from the array leaves {1, 3, 1, 5}.
Removal of {1, 3, 1} leaves {5}.
Removal of 5 makes the array empty.Input: arr[] = {1, 2, 3, 5, 3, 1}, N = 5
Output: 2
Explanation:
Removal of {3, 5, 3} leaves {1, 2, 1}.
Removal of {1, 2, 1} makes the array empty.
Approach:
We can use Interval Dynamic Programming to solve this problem. Follow the steps below to solve the problem:
dp [i][j]= minimum (dp [i][j], dp [i][k] + dp [k + 1][j]) where i ≤ k <j
Below is the implementation of the above approach:
C++
// C++ Program for the above approach #include <bits/stdc++.h> using namespace std; int dp[550][550]; int minSubarrayRemoval(vector<int>& arr) { int i, j, k, l; int n = arr.size(); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { dp[i][j] = n; } } for (i = 0; i < n; i++) { dp[i][i] = 1; } for (i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { dp[i][i + 1] = 1; } else { dp[i][i + 1] = 2; } } for (l = 2; l < n; l++) { for (i = 0; i + l < n; i++) { j = i + l; if (arr[i] == arr[j]) { dp[i][j] = dp[i + 1][j - 1]; } for (k = i; k < j; k++) { dp[i][j] = min( dp[i][j], dp[i][k] + dp[k + 1][j]); } } } return dp[0][n - 1]; } // Driver Program int main() { vector<int> arr = { 2, 3, 1, 2, 2, 1, 2 }; int ans = minSubarrayRemoval(arr); cout << ans << endl; } |
Java
// Java program for the above approach class GFG{ static int dp[][] = new int[550][550]; static int minSubarrayRemoval(int arr[]) { int i, j, k, l; int n = arr.length; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { dp[i][j] = n; } } for(i = 0; i < n; i++) { dp[i][i] = 1; } for(i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { dp[i][i + 1] = 1; } else { dp[i][i + 1] = 2; } } for(l = 2; l < n; l++) { for(i = 0; i + l < n; i++) { j = i + l; if (arr[i] == arr[j]) { dp[i][j] = dp[i + 1][j - 1]; } for(k = i; k < j; k++) { dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k + 1][j]); } } } return dp[0][n - 1]; } // Driver code public static void main (String[] args) { int arr [] = new int[]{ 2, 3, 1, 2, 2, 1, 2 }; int ans = minSubarrayRemoval(arr); System.out.println(ans); } } // This code is contributed by Pratima Pandey |
Python3
# Python3 program for the above approach def minSubarrayRemoval(arr): n = len(arr) dp = [] for i in range(n): l = [0] * n for j in range(n): l[j] = n dp.append(l) for i in range(n): dp[i][i] = 1 for i in range(n - 1): if (arr[i] == arr[i + 1]): dp[i][i + 1] = 1 else: dp[i][i + 1] = 2 for l in range(2, n): for i in range(n - l): j = i + l if (arr[i] == arr[j]): dp[i][j] = dp[i + 1][j - 1] for k in range(i, j): dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]) return dp[0][n - 1] # Driver code arr = [ 2, 3, 1, 2, 2, 1, 2 ] ans = minSubarrayRemoval(arr) print(ans) # This code is contributed by shubhamsingh10 |
C#
// C# program for the above approach using System; class GFG{ static int [,]dp = new int[550, 550]; static int minSubarrayRemoval(int []arr) { int i, j, k, l; int n = arr.Length; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) { dp[i, j] = n; } } for(i = 0; i < n; i++) { dp[i, i] = 1; } for(i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { dp[i, i + 1] = 1; } else { dp[i, i + 1] = 2; } } for(l = 2; l < n; l++) { for(i = 0; i + l < n; i++) { j = i + l; if (arr[i] == arr[j]) { dp[i, j] = dp[i + 1, j - 1]; } for(k = i; k < j; k++) { dp[i, j] = Math.Min(dp[i, j], dp[i, k] + dp[k + 1, j]); } } } return dp[0, n - 1]; } // Driver code public static void Main() { int []arr = new int[]{ 2, 3, 1, 2, 2, 1, 2 }; int ans = minSubarrayRemoval(arr); Console.Write(ans); } } // This code is contributed by Code_Mech |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum removals from array to make max - min <= K
- Minimum removals to make array sum even
- Minimum removals from array to make GCD greater
- Minimum removals to make array sum odd
- Longest subarray of non-empty cells after removal of at most a single empty cell
- Make palindromic string non-palindromic by rearranging its letters
- Minimum removals in a number to be divisible by 10 power raised to K
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum minutes needed to make the time palindromic
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Minimum size substring to be removed to make a given string palindromic
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Steps to make array empty by removing maximum and its right side
- Maximize the sum of selected numbers from an array to make it empty
- Make three non-empty sets with negative, positive and 0 products
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Make a palindromic string from given string
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

