Minimum number of bins required to place N items ( Using Best Fit algorithm )

Minimum number of bins required to place N items ( Using Best Fit algorithm )Given an array weight[] consisting of weights of N items and a positive integer C representing the capacity of each bin, the task is to find the minimum number of bins required such that all items are assigned to one of the bins.Examples:Input: weight[] = {4, 8, 1, 4, 2, 1}, C = 10Output: 2Explanation: The minimum number of bins required to accommodate all items is 2.The first bin contains the items with weights {4, 4, 2}.The second bin contains the items with weights {8, 1, 1}.Input: weight[] = {9, 8, 2, 2, 5, 4}, C = 10Output: 4Approach: The given problem can be solved by using the best-fit algorithm. The idea is to place the next item in the bin, where the smallest empty space is left. Follow the steps below to solve the problem:Initialize a variable, say count as 0 that stores the minimum number of bins required.Sort the given array weight[] in decreasing order.Initialize a multiset, say M to store the empty spaces left in the occupied bins presently.Traverse the array weight[] and for each element perform the following steps:If there exists the smallest empty space which is at least arr[i] is present in the M, then erase that space from M and insert the remaining free space to M.Otherwise, increment the count by 1 and insert the empty space of the new bin in M.After completing the above steps, print the value of count as the minimum number of bins required.Below is the implementation of the above approach:C++  #include using namespace std;  void bestFit(int arr[], int n, int W){            int count = 0;          sort(arr, arr + n, greater());              multiset M;          for (int i = 0; i < n; i++) {                          auto x = M.find(arr[i]);                          auto y = M.upper_bound(arr[i]);                                  if (x != M.end()) {            M.erase(x);        }                                  else if (y != M.end()) {            M.insert(*y - arr[i]);            M.erase(y);        }                                  else {            count++;            M.insert(W - arr[i]);        }    }          cout