Maximum array elements that can be removed along with its adjacent values to empty given array
Last Updated :
17 May, 2021
Given an array arr[] of size N. In each operation, pick an array element X and remove all array elements in the range [X – 1, X + 1]. The task is to find the maximum number of steps required such that no coins left in the array.
Examples:
Input: coins [] = {5, 1, 3, 2, 6, 7, 4}
Output: 4
Explanation:
Picking the coin coins[1] modifies the array arr[] = {5, 3, 6, 7, 4}.
Picking the coin coins[1] modifies the array arr[] = {5, 6, 7}
Picking the coin coins[0] modifies the array arr[] = {7}
Picking the coin coins[0] modifies the array arr[] = {}. Therefore, the required output is 4.
Input: coins [] = {6, 7, 5, 1}
Output: 3
Naive Approach: The simplest approach to solve this problem is to generate all possible permutation of the given array and for each permutation of the array, find the number of steps required to remove all the elements of the array by picking only the first element of the array in all possible step. Finally, print the maximum number of steps required to remove all the elements.
Time Complexity: O(N!)
Auxiliary Space: O(1)
Efficient approach: To optimize the above approach the idea is to pick an element from the array in such a way that in each step at most two elements from the array will be removed. Follow the steps below to solve the problem:
- Initialize a variable, say cntSteps to store the maximum count of steps required to remove all the coins from the arr[] array.
- Create a map, say Map to store the frequency of elements of the arr[] array in ascending order.
- Initialize a variable, say Min to store the smallest element of the Map.
- Traverse the Map and in each traversal remove the Min and (Min + 1) from Map and also increment the value of cntSteps by 1.
- Finally, print the value of cntSteps.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int maximumSteps( int arr[], int N)
{
map< int , int > Map;
for ( int i = 0; i < N; i++) {
Map[arr[i]]++;
}
int cntSteps = 0;
for ( auto i : Map) {
int X = i.first;
if (i.second > 0) {
cntSteps++;
Map[X] = 0;
if (Map[X + 1])
Map[X + 1] = 0;
}
}
return cntSteps;
}
int main()
{
int arr[] = { 5, 1, 3, 2, 6, 7, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maximumSteps(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maximumSteps( int arr[], int N)
{
Map<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
mp.put(arr[i],
mp.getOrDefault(arr[i], 0 ) + 1 );
}
int cntSteps = 0 ;
for (Map.Entry<Integer, Integer> it : mp.entrySet())
{
int X = it.getKey();
if (it.getValue() > 0 )
{
cntSteps++;
mp.replace(X, 0 );
if (mp.getOrDefault(X + 1 , 0 ) != 0 )
mp.replace(X + 1 , 0 );
}
}
return cntSteps;
}
public static void main(String args[])
{
int arr[] = { 5 , 1 , 3 , 2 , 6 , 7 , 4 };
int N = arr.length;
System.out.print(maximumSteps(arr, N));
}
}
|
Python3
def maximumSteps(arr, N):
Map = {}
for i in range (N):
Map [arr[i]] = Map .get(arr[i], 0 ) + 1
cntSteps = 0
for i in Map :
X = i
if ( Map [i] > 0 ):
cntSteps + = 1
Map [X] = 0
if (X + 1 in Map ):
Map [X + 1 ] = 0
return cntSteps
if __name__ = = '__main__' :
arr = [ 5 , 1 , 3 , 2 , 6 , 7 , 4 ]
N = len (arr)
print (maximumSteps(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int maximumSteps( int []arr, int N)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp.Add(arr[i], 1);
}
}
int cntSteps = 0;
foreach (KeyValuePair< int , int > it in mp)
{
int X = it.Key;
if (it.Value > 0)
{
cntSteps++;
}
}
return (cntSteps + 1) / 2;
}
public static void Main(String []args)
{
int []arr = { 5, 1, 3, 2, 6, 7, 4 };
int N = arr.Length;
Console.Write(maximumSteps(arr, N));
}
}
|
Javascript
<script>
function maximumSteps(arr, N)
{
var map = new Map();
for ( var i = 0; i < N; i++) {
if (map.has(arr[i]))
{
map.set(arr[i], map.get(arr[i])+1);
}
else
{
map.set(arr[i], 1);
}
}
var cntSteps = 0;
map.forEach((value, key) => {
var X = key;
if (value > 0) {
cntSteps++;
map.set(X, 0);
if (map.has(X + 1))
map.set(X+1, 0);
}
});
return cntSteps;
}
var arr = [5, 1, 3, 2, 6, 7, 4];
var N = arr.length;
document.write( maximumSteps(arr, N));
</script>
|
Time Complexity: O(N * Log(N))
Space Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...