Minimum length of X[] after removing at most K sub-arrays
Last Updated :
15 Nov, 2023
Given an array X[] of length N and an integer K. Then your task is to output minimum length of X[] after removing at most K number of sub-arrays, which satisfies the following conditions:
- Size of the sub-array must be greater than 1.
- First and last element of sub-array must be same.
Note: The sub-arrays which will satisfy the above condition will be non-intersecting. Formally, any sub-array which starts and ends with element A will not be overlapped with the any sub-array starting and ending with the element except A.
Examples:
Input: N = 9, X[] = {2, 3, 1, 1, 9, 34, 56, 9, 9}, K = 3
Output: 2
Explanation: There are two sub-arrays {1, 1} and {9, 34, 56, 9, 9}, which start and end with same element having size greater than 1. Since we can remove at most K = 3 sub-arrays, but two of such kind are present. Therefore, after removing these two sub-arrays, the minimum length of the remained X[] will be = 2.
Input: N = 4, X[] = {1, 2, 3, 4}, X = 2
Output: 4
Explanation: There are no sub-arrays such that it follows the given conditions. Therefore, we can't remove any such sub-array. Hence the minimum length of X[] will be original as initial 4.
Approach: The problem can be solved by applying the Greedy approach.
We have to remove the K number of sub-arrays such that they follow the given conditions. As it is mentioned that sub-arrays must start and end with the same element. Thus we can get the first and last occurrence of an element from X[]. If the size of such sub-arrays is greater than 1 then store that length in a separate list. We can find the length of all such sub-arrays by iterating over X[] using a loop. After finding the length of all such sub-arrays, Sort them in any order(descending or ascending), and remove K number of subarrays of the biggest length from N, Formally, for minimizing the length of the remaining X[], we must remove the K sub-arrays having a maximum length. Now, the length of the remained X[] will be the minimum possible.
Below are the steps for the above approach:
- Create an ArrayList say list and copy the element of X[] into it.
- Create a HashMap say map for storing the length of sub-arrays.
- Run a loop from i = 0 to i < list size,
- Initialize a variable firstIndex = IndexOf(current_element)
- Initialize a variable lastIndex = lastIndexOf(current_element)
- Check if(firstIndex == lastIndex)
- else if the size is greater than 1, then put the length in Map
- map.put(current_element, lastIndex-firstIndex+1)
- Create an ArrayList say length for storing frequencies from the map.
- Sort the array length.
- Iterate the length array from index i = size of length array to i ≥ 0 and k > 0 and subtract K biggest lengths from N,
N = N - lengths.get(i) and decrement the value of K by 1. - Print the value of N.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Method for printing minimum length
void remainedElements(int N, int K, vector<int>& X)
{
// Map initialized for storing
// lentgth of sub subarrays along
// with the element
unordered_map<int, int> map;
// Loop for initilizing map with
// length of such sub-arrays
for (int i = 0; i < X.size(); i++) {
// Getting first index of
// current element
int firstIndex = find(X.begin(), X.end(), X[i]) - X.begin();
// Getting last index of
// current element
int lastIndex = find(X.rbegin(), X.rend(), X[i]) - X.rbegin();
// Convert last index to
// actual index in X
lastIndex = X.size() - 1 - lastIndex;
// If firstIndex is equal to
// Last index Formally, there
// is only one element of such
// value Then ignore it, can't
// be considered as sub-array
// because size must be
// greater than 1
if (firstIndex == lastIndex) {
continue;
}
// If size is greater than 1,
// Then putting length in Map
else {
map[X[i]] = lastIndex - firstIndex + 1;
}
}
// Vector for storing lengths
// from map
vector<int> lengths;
for (auto it = map.begin(); it != map.end(); ++it) {
lengths.push_back(it->second);
}
// Sorting sub-array's length
sort(lengths.begin(), lengths.end());
// Removing K number of such
// sub-arrays with biggest size
// formally, Iterating backward
// in list of sorted lengths
for (int i = lengths.size() - 1; i >= 0 && K > 0;
i--) {
N -= lengths[i];
K--;
}
// Printing minimum length after
// removing such K sub-arrays
cout << N << endl;
}
// Driver Function
int main()
{
// Inputs
int N = 9;
int K = 3;
vector<int> X = { 12, 3, 1, 1, 2, 2, 2, 2, 5 };
// Function call for printing
// length after removing such
// K number of sub-arrays
remainedElements(N, K, X);
return 0;
}
Java
// java code to implement the approach
import java.util.*;
public class GFG {
// Driver Function
public static void main(String[] args)
{
// Inputs
int N = 9;
int K = 3;
int X[] = { 12, 3, 1, 1, 2, 2, 2, 2, 5 };
// Function call for printing
// length after removing such
// K number of sub-arrays
RemainedElements(N, K, X);
}
// Method for printing minimum length
static void RemainedElements(int N, int K, int[] X)
{
// ArrayList for storing element
// of X[] because it will be
// convienient to find First and
// last index of an element using
// in-built func. such as
// indexOf(), lastIndexOf()
ArrayList<Integer> list = new ArrayList<>();
// Loop for copying elements
// from X[] to list
for (int x : X)
list.add(x);
// Map initialized for storing
// lentgth of sub subarrays along
// with the element
HashMap<Integer, Integer> map = new HashMap<>();
// Loop for initilizing map with
// length of such sub-arrays
for (int i = 0; i < list.size(); i++) {
// Getting first index of
// current element
int firstIndex = list.indexOf(list.get(i));
// Getting last index of
// current element
int lastIndex = list.lastIndexOf(list.get(i));
// If firstIndex is equal to
// Last index Formally, there
// is only one element of such
// value Then ignore it, can't
// be considered as sub-array
// because size must be
// greater than 1
if (firstIndex == lastIndex) {
continue;
}
// If size is greater than 1,
// Then putting length in Map
else {
map.put(list.get(i),
lastIndex - firstIndex + 1);
}
}
// List for storing lengths
// from map
ArrayList<Integer> lengths
= new ArrayList<>(map.values());
// Sorting sub-array's length
lengths.sort(null);
// Removing K number of such
// sub-arrays with biggest size
// formally, Iterating backward
// in list of sorted lengths
for (int i = lengths.size() - 1; i >= 0 && K > 0;
i--) {
N = N - lengths.get(i);
K--;
}
// Printing minimum length after
// removing such K sub-arrays
System.out.println(N);
}
}
Python3
import collections
# Method for printing minimum length
def remainedElements(N, K, X):
# Map initialized for storing
# lentgth of sub subarrays along
# with the element
map = collections.defaultdict(int)
# Loop for initilizing map with
# length of such sub-arrays
for i in range(len(X)):
# Getting first index of
# current element
firstIndex = X.index(X[i])
# Getting last index of
# current element
lastIndex = len(X) - 1 - X[::-1].index(X[i])
# If firstIndex is equal to
# Last index Formally, there
# is only one element of such
# value Then ignore it, can't
# be considered as sub-array
# because size must be
# greater than 1
if firstIndex == lastIndex:
continue
# If size is greater than 1,
# Then putting length in Map
else:
map[X[i]] = lastIndex - firstIndex + 1
# Vector for storing lengths
# from map
lengths = list(map.values())
# sorting sub-array's length
lengths.sort()
# Removing K number of such
# sub-arrays with biggest size
# formally, Iterating backward
# in list of sorted lengths
for i in range(len(lengths) - 1, -1, -1):
N -= lengths[i]
K -= 1
if K == 0:
break
# Printing minimum length after
# removing such K sub-arrays
print(N)
# Driver Function
N = 9
K = 3
X = [12, 3, 1, 1, 2, 2, 2, 2, 5]
remainedElements(N, K, X)
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
// Method for printing minimum length
static void RemainedElements(int N, int K, List<int> X)
{
// Dictionary initialized for storing
// length of sub subarrays along with the element
Dictionary<int, int> map = new Dictionary<int, int>();
// Loop for initializing map with
// the length of such sub-arrays
for (int i = 0; i < X.Count; i++)
{
// Getting first index of current element
int firstIndex = X.IndexOf(X[i]);
// Getting last index of current element
int lastIndex = X.LastIndexOf(X[i]);
// If firstIndex is equal to
// last index, there is only one element of such value
// Then ignore it, it can't be considered as sub-array
// because size must be greater than 1
if (firstIndex == lastIndex)
{
continue;
}
// If size is greater than 1,
// then put length in the map
else
{
map[X[i]] = lastIndex - firstIndex + 1;
}
}
// List for storing lengths from map
List<int> lengths = new List<int>();
foreach (var kvp in map)
{
lengths.Add(kvp.Value);
}
// Sorting sub-array's length
lengths.Sort();
// Removing K number of such sub-arrays with the biggest size
// Formally, iterating backward in the list of sorted lengths
for (int i = lengths.Count - 1; i >= 0 && K > 0; i--)
{
N -= lengths[i];
K--;
}
// Printing the minimum length after removing such K sub-arrays
Console.WriteLine(N);
}
// Driver Function
public static void Main()
{
// Inputs
int N = 9;
int K = 3;
List<int> X = new List<int> { 12, 3, 1, 1, 2, 2, 2, 2, 5 };
// Function call for printing
// length after removing such
// K number of sub-arrays
RemainedElements(N, K, X);
}
}
JavaScript
function GFG(N, K, X) {
// Map initialized for storing the length of sub-arrays along with
// the element
const map = new Map();
// Loop for initializing the map with
// the length of sub-arrays
for (let i = 0; i < X.length; i++) {
// Getting the first index of current element
const firstIndex = X.indexOf(X[i]);
// Getting the last index of current element
const lastIndex = X.lastIndexOf(X[i]);
// If firstIndex is equal to the last index
// ignore it (size must be greater than 1)
if (firstIndex === lastIndex) {
continue;
} else {
// Calculate the length and store it in the map
map.set(X[i], lastIndex - firstIndex + 1);
}
}
// Get an array of sub-array lengths from the map
const lengths = Array.from(map.values());
// Sort the sub-array lengths
lengths.sort((a, b) => a - b);
// Removing K number of sub-arrays with biggest size
for (let i = lengths.length - 1; i >= 0 && K > 0; i--) {
N -= lengths[i];
K--;
}
// Printing the minimum length after removing K sub-arrays
console.log(N);
}
// Driver Function
function main() {
// Inputs
const N = 9;
const K = 3;
const X = [12, 3, 1, 1, 2, 2, 2, 2, 5];
// Function call for calculating the minimum length after
// removing K sub-arrays
GFG(N, K, X);
}
main();
Time Complexity: O(N * LogN)
Space Complexity: O(N)
Similar Reads
Maximum subarray sum possible after removing at most K array elements
Given an array arr[] of size N and an integer K, the task is to find the maximum subarray sum by removing at most K elements from the array. Examples: Input: arr[] = { -2, 1, 3, -2, 4, -7, 20 }, K = 1 Output: 26 Explanation: Removing arr[5] from the array modifies arr[] to { -2, 1, 3, -2, 4, 20 } Su
15+ min read
Minimum bitwise OR after removing at most K elements from given Array
Given an array A[] of length N, the task is to find the minimum possible value of bitwise OR of the elements after removing at most K elements. Examples: Input: A = {1, 10, 9, 4, 5, 16, 8}, K = 3Output: 11Explanation: Remove 4, 5, 16 for the given array.The remaining elements are {1, 10, 9, 5}. The
7 min read
Minimize Sum of an Array by at most K reductions
Given an array of integers arr[] consisting of N integers, the task is to minimize the sum of the given array by performing at most K operations, where each operation involves reducing an array element arr[i] to floor(arr[i]/2). Examples : Input: N = 4, a[] = {20, 7, 5, 4}, K = 3 Output: 17 Explanat
12 min read
Minimize the sum of MEX by removing all elements of array
Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Minimum sum possible by removing all occurrences of any array element
Given an array arr[] consisting of N integers, the task is to find the minimum possible sum of the array by removing all occurrences of any single array element. Examples: Input: N = 4, arr[] = {4, 5, 6, 6}Output: 9Explanation: All distinct array elements are {4, 5, 6}. Removing all occurrences of 4
6 min read
Minimum length subarray of 1s in a Binary Array
Given binary array. The task is to find the length of subarray with minimum number of 1s.Note: It is guaranteed that there is atleast one 1 present in the array.Examples : Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1} Output : 3 Minimum length subarray of 1s is {1, 1}.Input : arr[] = {0, 0, 1
8 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
Sum of XOR of all sub-arrays of length K
Given an array of length n ( n > k), we have to find the sum of xor of all the elements of the sub-arrays which are of length k.Examples: Input : arr[]={1, 2, 3, 4}, k=2 Output :Sum= 11 Sum = 1^2 + 2^3 + 3^4 = 3 + 1 + 7 =11Input :arr[]={1, 2, 3, 4}, k=3 Output :Sum= 5 Sum = 1^2^3 + 2^3^4 = 0 + 5
8 min read
Minimum Value X to Delete All Arrays
Given 2d array A[][] of size N*M. Your Task is to choose the minimum possible number X to perform the following operation. In one operation choose any array i from N arrays and delete the first element, if it is less than X, and increase X by 1. Keep performing this operation until the array becomes
12 min read
Minimum common sum from K arrays after removing some part of their suffix
Given K (K > 2) arrays of different sizes in a 2D list arr[][] where elements in each array are non-negative. Find the minimum common sum of K arrays after removing part of the suffix(possibly none) from each array. Examples: Input: K = 3, arr = {{5, 2, 4}, {1, 4, 1, 1}, {2, 3}}Output: 5Explanati
7 min read