Cost required to empty a given array by repeated removal of maximum obtained by given operations
Last Updated :
23 Apr, 2023
Given an array arr[] consisting of N integers, the task is to find the cost to remove all array elements after performing the following operations in the specified order any number of times:
Examples:
Input: arr[] = {1, 6, 7, 4, 2, 5, 3}
Output: 16
Explanation:
Step 1: Current maximum element = 7. Removing 7 and reducing each element by 1 modifies the array to {0, 5, 3, 1, 4, 2}. Cost = 7.
Step 2: Current maximum element = 5. Removing 5 and reducing each element by 1 modifies the array to {2, 0, 3, 1}. Cost = 7 + 5 = 12.
Step 3: Current maximum = 3. Removing 3 and reducing each element by 1 modifies the array to {1, 1}. Cost = 12 + 3 = 15
Step 4: Final cost obtained = 15 + 1 = 16
Input: arr[] = {6, 4, 6, 1}
Output: 13
Naive Approach: The simplest approach to solve the problem is as follows:
- Find the maximum from the given array
- Add it to the cost after removing it from the array.
- Once the maximum element is removed, reduce all remaining array elements by 1. Remove all elements reducing to 0.
- Repeat the above steps until all array elements are removed.
- Finally, print the value of cost.
C++
#include <bits/stdc++.h>
using namespace std;
int findCost(vector< int >& arr, int n)
{
long long cost = 0;
while (!arr.empty()) {
int max_elem = *max_element(arr.begin(), arr.end());
cost += max_elem;
arr.erase(find(arr.begin(), arr.end(), max_elem));
for ( int i = 0; i < arr.size(); i++) {
arr[i]--;
}
arr.erase( remove (arr.begin(), arr.end(), 0),
arr.end());
}
return cost;
}
int main()
{
vector< int > arr = { 1, 6, 7, 4, 2, 5, 3 };
int n = arr.size();
int cost = findCost(arr, n);
cout << cost << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
static int findCost(ArrayList<Integer> arr, int n)
{
long cost = 0 ;
while (!arr.isEmpty()) {
int max_elem = Collections.max(arr);
cost += max_elem;
arr.remove(arr.indexOf(max_elem));
for ( int i = 0 ; i < arr.size(); i++) {
arr.set(i, arr.get(i) - 1 );
}
arr.removeIf(e -> e == 0 );
}
return ( int )cost;
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<>(
Arrays.asList( 1 , 6 , 7 , 4 , 2 , 5 , 3 ));
int n = arr.size();
int cost = findCost(arr, n);
System.out.println(cost);
}
}
|
Python3
def findCost(arr):
cost = 0
while arr:
max_elem = max (arr)
cost + = max_elem
arr.remove(max_elem)
for i in range ( len (arr)):
arr[i] - = 1
arr = [x for x in arr if x ! = 0 ]
return cost
if __name__ = = '__main__' :
arr = [ 1 , 6 , 7 , 4 , 2 , 5 , 3 ]
cost = findCost(arr)
print (cost)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static int FindCost(List< int > arr, int n) {
long cost = 0;
while (arr.Count > 0) {
int max_elem = arr.Max();
cost += max_elem;
arr.Remove(max_elem);
for ( int i = 0; i < arr.Count; i++) {
arr[i]--;
}
arr.RemoveAll(elem => elem == 0);
}
return ( int ) cost;
}
static void Main( string [] args) {
List< int > arr = new List< int >() { 1, 6, 7, 4, 2, 5, 3 };
int n = arr.Count;
int cost = FindCost(arr, n);
Console.WriteLine(cost);
}
}
|
Javascript
function findCost(arr) {
let cost = 0;
while (arr.length > 0) {
let max_elem = Math.max(...arr);
cost += max_elem;
arr.splice(arr.indexOf(max_elem), 1);
for (let i = 0; i < arr.length; i++) {
arr[i]--;
}
arr = arr.filter(item => item !== 0);
}
return cost;
}
let arr = [1, 6, 7, 4, 2, 5, 3];
let cost = findCost(arr);
console.log(cost);
|
Time Complexity: O(N2), where N is the size of the given array.
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to observe that every ith greatest element in the array is added arr[i] – i times to the cost where i is the index of each element arr[i]. Follow the below steps to solve the above problem:
- Sort the given array in decreasing order.
- Traverse the array and add the value arr[i] – i for each array element to cost if it is greater than 0.
- Once the above steps are completed, print the value of cost.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findCost( int * a, int n)
{
sort(a, a + n, greater< int >());
int count = 0;
for ( int j = 0; j < n; j++) {
int p = a[j] - j;
a[j] = 0;
if (p < 0) {
p = 0;
continue ;
}
count += p;
}
return count;
}
int main()
{
int arr[] = { 1, 6, 7, 4, 2, 5, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findCost(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findCost(Integer [] a, int n)
{
Arrays.sort(a, Collections.reverseOrder());
int count = 0 ;
for ( int j = 0 ; j < n; j++)
{
int p = a[j] - j;
a[j] = 0 ;
if (p < 0 )
{
p = 0 ;
continue ;
}
count += p;
}
return count;
}
public static void main(String[] args)
{
Integer arr[] = { 1 , 6 , 7 ,
4 , 2 , 5 , 3 };
int N = arr.length;
System.out.print(findCost(arr, N));
}
}
|
Python3
def findCost(a, n):
a.sort(reverse = True )
count = 0
for j in range (n):
p = a[j] - j
a[j] = 0
if (p < 0 ):
p = 0
continue
count + = p
return count
arr = [ 1 , 6 , 7 , 4 , 2 , 5 , 3 ]
N = len (arr)
print (findCost(arr, N))
|
C#
using System;
class GFG{
static int findCost( int []a, int n)
{
Array.Sort(a);
Array.Reverse(a);
int count = 0;
for ( int j = 0; j < n; j++)
{
int p = a[j] - j;
a[j] = 0;
if (p < 0)
{
p = 0;
continue ;
}
count += p;
}
return count;
}
public static void Main(String[] args)
{
int []arr = {1, 6, 7,
4, 2, 5, 3};
int N = arr.Length;
Console.Write(findCost(arr, N));
}
}
|
Javascript
function findCost(a, n)
{
a.sort((x, y) => y - x);
var count = 0;
for ( var j = 0; j < n; j++)
{
var p = a[j] - j;
a[j] = 0;
if (p < 0) {
p = 0;
continue ;
}
count += p;
}
return count;
}
var arr = [1, 6, 7, 4, 2, 5, 3];
var N = arr.length;
console.log(findCost(arr, N));
|
Time Complexity: O(NlogN), where N is the size of the given array.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...