- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimize cost to reduce Array if for choosing every 2 elements, 3rd one is chosen for free
We are given an array it this problem and we have to remove all the elements of the array with the minimum cost required. We have to remove two elements at a time and add them to the total cost. Also, we can remove the third number without any cost if we remove two elements and the third element's value is at most equal to the minimum of them. Also, it is given that the given array will be of a size greater than 1.
Sample Examples
Input
int arr[] = {7, 6, 5, 2, 9, 2};
Output
23
Explanation: We can remove 9 and 7 together to remove the 6 and this will cost us 16. Then we can remove 5 and 2 together and will remove another 2 for free with a total cost of this operation of 7 and an overall cost of 23.
Input
int arr[] = {1, 2, 3, 4};
Output
10
Explanation: We can remove 4 and 3 together and then remove 2 but this will lead to the first element 1 single there and then we cannot remove it. So we will not remove 2 with the 4 and 3 and removing it will 1 bring us the total cost of 10.
Approach 1: Using Sorting
In this approach, we will sort the array and then traverse it from the back side. We will pass the elements in the group of three and add the cost of the first two only until we reach 4 or two elements then we will add the elements in the group of two only.
Example
#include <bits/stdc++.h> using namespace std; // function to find the required minimum cost int requiredCost(int arr[], int len){ // sort the given array by using the stl sort function sort(arr, arr + len); int cost = 0; // variable to store the cost traversing over the array from the back side for (int i = len- 1; i>= 0; i--){ // i is 3 or 1 means 4 and 2 elements left we can only choose in groups here and cannot remove third if(i == 3 || i == 1){ cost += arr[i] + arr[i-1]; i--; } else{ // we will lose the two elements that is ith and i-1th cost += arr[i] + arr[i-1]; i -= 2; } } return cost; // returning the total cost } int main(){ int arr[] = { 6, 5, 7, 9, 2, 2 }; // given array int len = sizeof(arr)/ sizeof(arr[0]); // getting length of array cout<<"The minimum cost to remove all the elements of the array is "<< requiredCost(arr, len)<<endl; return 0; }
Output
The minimum cost to remove all the elements of the array is 23
Time and Space Complexity
The time complexity of the above code is O(N*log(N)) where N is the number of elements in the given array. We are using the in-built sorting function costs us the logarithmic factor.
The space complexity of the above code is O(1) or constant as we are not using any extra space here.
Approach 2: Using Map
In this program we will use the map and get the elements from the last of map, each time we will get the element in group of 3 as we are doing in the previous code and then for the 4 and 2 element we will make the group of 2 only.
Example
#include <bits/stdc++.h> using namespace std; int requiredCost(int arr[], int len){ int cost = 0; // variable to store the cost map<int,int>mp; // traversing over the array adding elements to the map for(int i=0; i<len; i++){ mp[arr[i]]++; } // traversing over the map int rem_elements = len; // variable to track the remaining variables // traversing over the map until rem_elements exits while(rem_elements > 0){ if(rem_elements == 4 || rem_elements == 2){ auto it = mp.rbegin(); if(it->second > 1){ // remove 2 elements cost += 2*it->first; it->second -= 2; rem_elements -= 2; // removed 2 elements if(it->second == 0){ mp.erase(it->first); // remove from map } } else{ // remove current element cost += it->first; mp.erase(it->first); it = mp.rbegin(); cost += it->first; if(it->second == 1){ mp.erase(it->first); } rem_elements -= 2; // removed 2 elements } } else{ // now we can remove three elements int k = 3; while(k > 0){ auto it = mp.rbegin(); if(k > 1){ cost += it->first; } it->second--; if(it->second == 0){ mp.erase(it->first); } k--; } rem_elements -= 3; } } return cost; // returning the total cost } // main function int main(){ int arr[] = { 6, 5, 7, 9, 2, 2 }; // given array int len = sizeof(arr)/ sizeof(arr[0]); // getting length of arry // calling to the function cout<<"The minimum cost to remove all the elements of the array is "<< requiredCost(arr, len)<<endl; return 0; }
Output
The minimum cost to remove all the elements of the array is 23
Time and Space Complexity
The time complexity of the above code is O(N*log(N)), as we are using the map to store, excess, and delete the elements.
The space complexity of the above code is O(N), where N is the size of the given array and the extra space factor is due to the map.
Conclusion
In this tutorial, we have implemented a program to get the minimum cost to remove the elements from the array in the group of 3 if possible, otherwise in the group of 2 where removing elements in the group of 3 will cost only the sum of the maximum two elements. We have implemented two programs with the time complexity of O(N*log(N)), and O(1) and O(N) space complexity.