Minimum length of the reduced Array formed using given operations
Last Updated :
10 Jan, 2023
Given an array arr of length N, the task is to minimize its length by performing following operations:
- Remove any adjacent equal pairs, ( i.e. if arr[i] = arr[i+1]) and replace it with single instance of arr[i] + 1.
- Each operation decrements the length of the array by 1.
- Repeat the operation till no more reductions can be made.
Examples:
Input: arr = {3, 3, 4, 4, 4, 3, 3}
Output: 2
Explanation:
Merge the first two 3s and replace them by 4. Updated array: {4, 4, 4, 4, 3, 3}
Merge the first two 4s and replace them by 5. Updated array: {5, 4, 4, 3, 3}
Merge the two 4s and replace them by 5. Updated array: {5, 5, 3, 3}
Merge the two 5s and replace them by 6. Updated array: {6, 3, 3}
Merge the two 3s and replace them by 4. Updated array: {6, 4}
Hence, the minimum length of the reduced array = 2
Input: arr = {4, 3, 2, 2, 3}
Output: 2
Explanation:
Merge the two 2s and replace them by 3. Updated array: {4, 3, 3, 3}
Merge the first two 3s and replace them by 4. Updated array: {4, 4, 3}
Merge the two 4s and replace them by 5. Updated array: {5, 3}
Hence, the minimum length of the reduced array = 2
Approach: The above mentioned problem can be solved using Dynamic Programming. It can be observed that each element in the final array will be the result of the replacement of a number of elements on the corresponding segment. So our goal is to find the minimal partition of the array on segments, where each segment can be converted to a single element by a series of operations.
Let us define the following dynamic programming table state:
dp[i][j] = value of the single remaining element
when the subarray from index i to j is reduced by a series of operations or is equal to -1 when the subarray can't be reduced to a single element.
For computing dp[i][j]:
- If i = j, dp[i][j] = a[i]
- Iterate from [i, j-1], let the traversing index be k (i <= k < j). For any k if dp[i][k] = dp[k+1][j], this means that subarray [i, j] can be divided into two parts and both the parts have same final value, so these two parts can be combined i.e. dp[i][j] = dp[i][k] + 1.
For computing minimum partitions, we will create another dp table in which the final result is stored. This table has the following state:
dp1[i] = minimum partition of subarray [1: i]
which is the minimal size of array till i after above operations are performed.
Below is the implementation of the above approach:
CPP
// C++ implementation to find the
// minimum length of the array
#include <bits/stdc++.h>
using namespace std;
// Function to find the
// length of minimized array
int minimalLength(int a[], int n)
{
// Creating the required dp tables
int dp[n + 1][n + 1], dp1[n];
int i, j, k;
// Initialising the dp table by -1
memset(dp, -1, sizeof(dp));
for (int size = 1; size <= n; size++) {
for (i = 0; i < n - size + 1; i++) {
j = i + size - 1;
// base case
if (i == j)
dp[i][j] = a[i];
else {
for (k = i; k < j; k++) {
// Check if the two subarray
// can be combined
if (dp[i][k] != -1
&& dp[i][k] == dp[k + 1][j])
dp[i][j] = dp[i][k] + 1;
}
}
}
}
// Initialising dp1 table with max value
for (i = 0; i < n; i++)
dp1[i] = 1e7;
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
// Check if the subarray can be
// reduced to a single element
if (dp[j][i] != -1) {
if (j == 0)
dp1[i] = 1;
// Minimal partition
// of [1: j-1] + 1
else
dp1[i] = min(
dp1[i],
dp1[j - 1] + 1);
}
}
}
return dp1[n - 1];
}
// Driver code
int main()
{
int n = 7;
int a[n] = { 3, 3, 4, 4, 4, 3, 3 };
cout << minimalLength(a, n);
return 0;
}
Java
// Java implementation to find the
// minimum length of the array
import java.util.*;
class GFG{
// Function to find the
// length of minimized array
static int minimalLength(int a[], int n)
{
// Creating the required dp tables
int [][]dp = new int[n + 1][n + 1];
int []dp1 = new int[n];
int i, j, k;
// Initialising the dp table by -1
for (i = 0; i < n + 1; i++) {
for (j = 0; j < n + 1; j++) {
dp[i][j] = -1;
}
}
for (int size = 1; size <= n; size++) {
for (i = 0; i < n - size + 1; i++) {
j = i + size - 1;
// base case
if (i == j)
dp[i][j] = a[i];
else {
for (k = i; k < j; k++) {
// Check if the two subarray
// can be combined
if (dp[i][k] != -1
&& dp[i][k] == dp[k + 1][j])
dp[i][j] = dp[i][k] + 1;
}
}
}
}
// Initialising dp1 table with max value
for (i = 0; i < n; i++)
dp1[i] = (int) 1e7;
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
// Check if the subarray can be
// reduced to a single element
if (dp[j][i] != -1) {
if (j == 0)
dp1[i] = 1;
// Minimal partition
// of [1: j-1] + 1
else
dp1[i] = Math.min(
dp1[i],
dp1[j - 1] + 1);
}
}
}
return dp1[n - 1];
}
// Driver code
public static void main(String[] args)
{
int n = 7;
int a[] = { 3, 3, 4, 4, 4, 3, 3 };
System.out.print(minimalLength(a, n));
}
}
// This code contributed by Princi Singh
Python3
# Python3 implementation to find the
# minimum length of the array
import numpy as np
# Function to find the
# length of minimized array
def minimalLength(a, n) :
# Creating the required dp tables
# Initialising the dp table by -1
dp = np.ones((n + 1,n + 1)) * -1;
dp1 = [0]*n;
for size in range(1, n + 1) :
for i in range( n - size + 1) :
j = i + size - 1;
# base case
if (i == j) :
dp[i][j] = a[i];
else :
for k in range(i,j) :
# Check if the two subarray
# can be combined
if (dp[i][k] != -1 and dp[i][k] == dp[k + 1][j]) :
dp[i][j] = dp[i][k] + 1;
# Initialising dp1 table with max value
for i in range(n) :
dp1[i] = int(1e7);
for i in range(n) :
for j in range(i + 1) :
# Check if the subarray can be
# reduced to a single element
if (dp[j][i] != -1) :
if (j == 0) :
dp1[i] = 1;
# Minimal partition
# of [1: j-1] + 1
else :
dp1[i] = min(
dp1[i],
dp1[j - 1] + 1);
return dp1[n - 1];
# Driver code
if __name__ == "__main__" :
n = 7;
a = [ 3, 3, 4, 4, 4, 3, 3 ];
print(minimalLength(a, n));
# This code is contributed by Yash_R
C#
// C# implementation to find the
// minimum length of the array
using System;
class GFG{
// Function to find the
// length of minimized array
static int minimalLength(int []a, int n)
{
// Creating the required dp tables
int [,]dp = new int[n + 1, n + 1];
int []dp1 = new int[n];
int i, j, k;
// Initialising the dp table by -1
for (i = 0; i < n + 1; i++) {
for (j = 0; j < n + 1; j++) {
dp[i, j] = -1;
}
}
for (int size = 1; size <= n; size++) {
for (i = 0; i < n - size + 1; i++) {
j = i + size - 1;
// base case
if (i == j)
dp[i, j] = a[i];
else {
for (k = i; k < j; k++) {
// Check if the two subarray
// can be combined
if (dp[i, k] != -1
&& dp[i, k] == dp[k + 1, j])
dp[i, j] = dp[i, k] + 1;
}
}
}
}
// Initialising dp1 table with max value
for (i = 0; i < n; i++)
dp1[i] = (int) 1e7;
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
// Check if the subarray can be
// reduced to a single element
if (dp[j, i] != -1) {
if (j == 0)
dp1[i] = 1;
// Minimal partition
// of [1: j-1] + 1
else
dp1[i] = Math.Min(
dp1[i],
dp1[j - 1] + 1);
}
}
}
return dp1[n - 1];
}
// Driver code
public static void Main(string[] args)
{
int n = 7;
int []a = { 3, 3, 4, 4, 4, 3, 3 };
Console.Write(minimalLength(a, n));
}
}
// This code is contributed by Yash_R
JavaScript
<script>
// Javascript implementation to find the
// minimum length of the array
// Function to find the
// length of minimized array
function minimalLength(a, n)
{
// Creating the required dp t0ables
// Initialising the dp table by -1
var i, j, k;
var dp = Array(n+1).fill(Array(n+1).fill(-1));
var dp1 = Array(n).fill(0);
for (var size = 1; size <= n; size++) {
for (i = 0; i < n - size + 1; i++) {
j = i + size - 1;
// base case
if (i == j)
dp[i][j] = a[i];
else {
for (k = i; k < j; k++) {
// Check if the two subarray
// can be combined
if (dp[i][k] != -1
&& dp[i][k] == dp[k + 1][j])
dp[i][j] = dp[i][k] + 1;
}
}
}
}
// Initialising dp1 table with max value
for (i = 0; i < n; i++)
dp1[i] = 1000000000;
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
// Check if the subarray can be
// reduced to a single element
if (dp[j][i] != -1) {
if (j == 0)
dp1[i] = 2;
// Minimal partition
// of [1: j-1] + 1
else
dp1[i] = Math.min(dp1[i], dp1[j - 1] + 1);
}
}
}
return dp1[n - 1];
}
// Driver code
var n = 7;
var a = [ 3, 3, 4, 4, 4, 3, 3 ];
document.write(minimalLength(a, n));
// This code is contributed by rrrtnx.
</script>
Time complexity: O(N3)
Auxiliary Space: O(N2), where N is the size of the given array.
Similar Reads
Minimum operations of given type required to empty given array
Given an array arr[] of size N, the task is to find the total count of operations required to remove all the array elements such that if the first element of the array is the smallest element, then remove that element, otherwise move the first element to the end of the array. Examples: Input: A[] =
14 min read
Minimum number of given operations required to reduce the array to 0 element
Given an array arr[] of N integers. The task is to find the minimum number of given operations required to reduce the array to 0 elements. In a single operation, any element can be chosen from the array and all of its multiples get removed including itself.Examples: Input: arr[] = {2, 4, 6, 3, 4, 6,
6 min read
Minimum divide by 2 operations required to make GCD odd for given Array
Given an array arr[] of N positive integers, the task is to find the minimum number of operations required to make the GCD of array element odd such that in each operation an array element can be divided by 2. Examples: Input: arr[] = {4, 6}Output: 1Explanation:Below are the operations performed:Ope
5 min read
Minimum operations to make all elements equal using the second array
Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of: A[i] = A[i] - B[i], 0 <= i <= n-1 Note: If it's not possible to make array elements equal print -1.Examples: Input: A[] =
7 min read
Minimum cost to equal all elements of array using two operation
Given an array arr[] of n positive integers. There are two operations allowed: Operation 1 : Pick any two indexes, increase value at one index by 1 and decrease value at another index by 1. It will cost a. Operation 2 : Pick any index and increase its value by 1. It will cost b. The task is to find
8 min read
Minimum number of operations required to delete all elements of the array
Given an integer array arr, the task is to print the minimum number of operations required to delete all elements of the array. In an operation, any element from the array can be chosen at random and every element divisible by it can be removed from the array. Examples: Input: arr[] = {2, 4, 6, 3, 5
9 min read
Minimum increment/decrement operations required on Array to satisfy given conditions
Given an array arr[] of size N, the task is to find the minimum number of increment or decrement operations required at any index i such that for each i (1 ? i < N) if the sum of elements at index from 1 to i is positive then the sum of elements from 1 to i + 1 must be negative or vice versa. Not
11 min read
Minimum element left from the array after performing given operations
Given an array arr[] of N integers, the task is to remove the elements from both the ends of the array i.e. in a single operation, either the first or the last element can be removed from the current remaining elements of the array. This operation needs to be performed in such a way that the last el
3 min read
Minimum operations to make Array sum at most S from given Array
Given an array arr[], of size N and an integer S, the task is to find the minimum operations to make the sum of the array less than or equal to S. In each operation: Any element can be chosen and can be decremented by 1, orCan be replaced by any other element in the array. Examples: Input: arr[]= {1
10 min read
Minimum Cost to make all array elements equal using given operations
Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t
12 min read