
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find minimum possible size of array with given rules for removing elements in C++
In this problem, we are given an array of n numbers and an integer value k. Our task is to Find minimum possible size of the array with given rules for removing elements.
Problem Description − we need to minimise the number of elements in the array. By using the follow deletion operation, The number of elements that can be removed at once is 3. The removal is possible if the three elements satisfy the two given conditions,
Cond 1 − Three elements should be adjacent.>
Cond 2 − the difference between two nearby elements is k, i.e. arr[i + 1] = arr[i] + k and arr[i+2] = arr[i+1] + k.
Let’s take an example to understand the problem,
Input
{4, 6, 8, 4, 1, 5 }, k = 2
Output
3
Explanation
One deletion operation is possible, for index 0, 1, 2.
Solution Approach
This problem is a bit tricky to solve as there can be indirect deletion operations possible that can be seen after one deletion operation is done. For example one we delete elements at 5, 6, 7. After this deletion, the new array might have elements 3, 4, 5 that now satisfy the condition for deletion. Such types of problems which have overlapping subproblems can be solved using a dynamic programming approach. For this, we will maintain a DP[] matrix to store results of the subproblems to access them later when required, this is called memorisation.
Program to illustrate the working of our solution,
Example
#include <bits/stdc++.h> using namespace std; #define MAX 1000 int DP[MAX][MAX]; int calcMinSize(int arr[], int start, int end, int k){ if (DP[start][end] != -1) return DP[start][end]; if ( (end-start + 1) < 3) return end-start +1; int minSize = 1 + calcMinSize(arr, start+1, end, k); for (int i = start+1; i<=end-1; i++){ for (int j = i+1; j <= end; j++ ){ if (arr[i] == (arr[start] + k) && arr[j] == (arr[start] + 2*k) && calcMinSize(arr, start+1, i-1, k) == 0 && calcMinSize(arr, i+1, j- 1, k) == 0) { minSize = min(minSize, calcMinSize(arr, j+1, end, k)); } } } return (DP[start][end] = minSize); } int main() { int arr[] = {4, 6, 8, 4, 1, 5 }; int n = sizeof(arr)/sizeof(arr[0]); int k = 2; memset(DP, -1, sizeof(DP)); cout<<"The minimum possible size of the array after removal is "<<calcMinSize(arr, 0, n-1, k); return 0; }
Output
The minimum possible size of the array after removal is 3
- Related Articles
- Find maximum of minimum for every window size in a given array in C++
- Print all possible combinations of r elements in a given array of size n in C++
- Minimum Possible value of |ai + aj – k| for given array and k in C++
- Program to find minimum possible difference of indices of adjacent elements in Python
- Removing empty array elements in PHP
- Program to find mean of array after removing some elements in Python
- Queries for counts of array elements with values in given range in C++
- Adding and Removing Elements in Perl Array
- Construct a distinct elements array with given size, sum and element upper bound in Python
- Find the maximum possible value of the minimum value of modified array in C++
- Removing duplicate elements from an array in PHP
- Program to find minimum string size that contains given substring in Python
- Compute the minimum of the masked array elements along a given axis in Numpy
- Removing redundant elements from array altogether - JavaScript
- Recursive Programs to find Minimum and Maximum elements of array in C++
