| 2 | using namespace std; |
| 3 | |
| 4 | int trips(vector<int> &weights){ |
| 5 | // initialize the variables and map to store frequencies of weights |
| 6 | int size = weights.size(); |
| 7 | int numTrips = 0; |
| 8 | map<int, int> freq; |
| 9 | |
| 10 | // store the frequencies of weights in the map |
| 11 | for(auto it : weights){ |
| 12 | freq[it]++; |
| 13 | } |
| 14 | |
| 15 | // iterate through the map and find the number of trips |
| 16 | for(auto elem : freq){ |
| 17 | int it = elem.second; |
| 18 | |
| 19 | // if frequency is 1, then we cannot make any trips |
| 20 | if(it == 1) return -1; |
| 21 | |
| 22 | // if frequency is multiple of 3, we can directly deliver it |
| 23 | if(it % 3 == 0){ |
| 24 | numTrips += it / 3; |
| 25 | } |
| 26 | |
| 27 | // if remainder is 2, we can make another trip with 2 of such elements |
| 28 | else if(it % 3 == 2){ |
| 29 | numTrips += (it - 2) / 3 + 1; |
| 30 | } |
| 31 | |
| 32 | // if remainder is 1, we can just remove 1 trip of 3, and in return make two trips of 2 |
| 33 | else{ |
| 34 | numTrips += (it - 1) / 3 + 1; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return numTrips; |
| 39 | } |
| 40 | |
| 41 | int main(){ |
| 42 | vector<int> weights = {2, 4, 6, 6, 4, 2, 4}; |