Open In App

Efficient waste bagging

Given a set of waste at certain positions in the Geek City, each with a weight value between 1.01 and 3.00, and the constraint is that a bag can carry at most 3.00 kg at one time, return the minimum number of bags required to carry all the waste efficiently.

Note: Please note that the weight values are represented in kilograms, and the objective is to optimize the number of bags needed while adhering to the weight limit of each bag. The weight of each bag should not exceed 3.00 kg.



Examples:

Input: wasteList = 1.30 1.40 1.50 1.60
Output: bags = 2
Explanation: bag1 with 1.30 and 1.60 and bag2 with 1.40 and 1.50.



Input: wasteList = 1.40 1.70 1.50 1.50
Output: bags = 3
Explanation: bag1 with 1.50 and 1.40, bag2 with 1.40 and bag3 with 1.50.

Efficient Approach: To solve the problem using Two Pointers follow the below idea:

Basic Observations:

  • As the weights in every position are in the range of 1.01 and 3.00 that means a bag can collect the waste from at max of 2 places.
  • All waste items weighing 2 kg or more must be placed in separate bags.

Below is the implementation of the above idea.




// C++ code for the above approach:
#include <algorithm>
#include <iostream>
#include <vector>
 
int MinBags(std::vector<double>& wasteList)
{
 
    // Sort the waste items in ascending
    // order to facilitate efficient grouping.
    std::sort(wasteList.begin(), wasteList.end());
 
    // Initialize variables for start and
    // end pointers, and answer
    // (minimum number of bags required).
    int answer = 0;
    int start = 0;
    int end = wasteList.size() - 1;
 
    // Iterate through the list using a
    // two-pointer approach.
    while (start <= end) {
        if (start == end) {
            answer += 1;
            break;
        }
 
        // If the sum of weights at start
        // and end is less than or equal
        // to 3, both items can be packed
        // into the same bag.
        if (wasteList[start] + wasteList[end] <= 3) {
            start += 1;
            end -= 1;
            answer += 1;
            continue;
        }
 
        // If the sum of weights exceeds
        // 3, pack the current item at
        // end separately in a new bag.
        answer += 1;
        end -= 1;
    }
 
    // Return the answer, representing the
    // minimum number of bags needed to
    // carry all the waste items efficiently.
    return answer;
}
 
// Drivers code
int main()
{
    std::vector<double> wasteList
        = { 1.30, 1.40, 1.50, 1.60 };
 
    // Call the MinBags function and print the result
    std::cout << MinBags(wasteList) << std::endl;
    return 0;
}




import java.util.ArrayList;
import java.util.Collections;
 
public class GFG {
 
    public static int minBags(ArrayList<Double> wasteList) {
        // Sort the waste items in ascending
        // order to facilitate efficient grouping.
        Collections.sort(wasteList);
 
        // Initialize variables for start and
        // end pointers, and answer
        // (minimum number of bags required).
        int answer = 0;
        int start = 0;
        int end = wasteList.size() - 1;
 
        // Iterate through the list using a
        // two-pointer approach.
        while (start <= end) {
            if (start == end) {
                answer += 1;
                break;
            }
 
            // If the sum of weights at start
            // and end is less than or equal
            // to 3, both items can be packed
            // into the same bag.
            if (wasteList.get(start) + wasteList.get(end) <= 3) {
                start += 1;
                end -= 1;
                answer += 1;
                continue;
            }
 
            // If the sum of weights exceeds
            // 3, pack the current item at
            // end separately in a new bag.
            answer += 1;
            end -= 1;
        }
 
        // Return the answer, representing the
        // minimum number of bags needed to
        // carry all the waste items efficiently.
        return answer;
    }
 
    public static void main(String[] args) {
        ArrayList<Double> wasteList = new ArrayList<>();
        wasteList.add(1.30);
        wasteList.add(1.40);
        wasteList.add(1.50);
        wasteList.add(1.60);
 
        // Call the minBags function and print the result
        System.out.println(minBags(wasteList));
    }
}




def MinBags(wasteList):
    # Sort the waste items in ascending order to facilitate efficient grouping.
    wasteList.sort()
 
    # Initialize variables for start and end pointers, and answer (minimum number of bags required).
    answer = 0
    start = 0
    end = len(wasteList) - 1
 
    # Iterate through the list using a two-pointer approach.
    while start <= end:
        if start==end:
            answer+=1
            break
        # If the sum of weights at start and end is less than or equal to 3,
        # both items can be packed into the same bag.
        if wasteList[start] + wasteList[end] <= 3:
            start += 1
            end -= 1
            answer += 1
            continue
 
        # If the sum of weights exceeds 3, pack the current item at end separately in a new bag.
        answer+=1
        end -= 1
 
    # Return the answer, representing the minimum number of bags needed to carry all the waste items efficiently.
    return answer
 
# Example usage:
wasteList = [1.30, 1.40, 1.50, 1.60]
print(MinBags(wasteList))




using System;
using System.Collections.Generic;
using System.Linq;
 
class Program
{
    static int MinBags(List<double> wasteList)
    {
        // Sort the waste items in ascending
        // order to facilitate efficient grouping.
        wasteList.Sort();
        // Initialize variables for start and
        // end pointers, and answer
        // (minimum number of bags required).
        int answer = 0;
        int start = 0;
        int end = wasteList.Count - 1;
        // Iterate through the list using a
        // two-pointer approach.
        while (start <= end)
        {
            if (start == end)
            {
                answer += 1;
                break;
            }
            // If the sum of weights at start
            // and end is less than or equal
            // to 3, both items can be packed
            // into the same bag.
            if (wasteList[start] + wasteList[end] <= 3)
            {
                start += 1;
                end -= 1;
                answer += 1;
                continue;
            }
            // If the sum of weights exceeds
            // 3, pack the current item at
            // end separately in a new bag.
            answer += 1;
            end -= 1;
        }
        // Return the answer, representing the
        // minimum number of bags needed to
        // carry all the waste items efficiently.
        return answer;
    }
 
    static void Main(string[] args)
    {
        List<double> wasteList = new List<double> { 1.30, 1.40, 1.50, 1.60 };
        // Call the MinBags function and print the result
        Console.WriteLine(MinBags(wasteList));
    }
}




function MinBags(wasteList) {
  // Sort the waste items in ascending order to facilitate efficient grouping.
  wasteList.sort();
 
  // Initialize variables for start and
  // end pointers, and answer (minimum number of bags required).
  let answer = 0;
  let start = 0;
  let end = wasteList.length - 1;
 
  // Iterate through the list using a two-pointer approach.
  while (start <= end) {
    if (start === end) {
      answer += 1;
      break;
    }
    // If the sum of weights at start and end is less than or equal to 3,
    // both items can be packed into the same bag.
    if (wasteList[start] + wasteList[end] <= 3) {
      start += 1;
      end -= 1;
      answer += 1;
      continue;
    }
 
    // If the sum of weights exceeds 3,
    // pack the current item at end separately in a new bag.
    answer += 1;
    end -= 1;
  }
 
  // Return the answer, representing the minimum number
  // of bags needed to carry all the waste items efficiently.
  return answer;
}
 
// Example usage:
const wasteList = [1.30, 1.40, 1.50, 1.60];
console.log(MinBags(wasteList));
 
// This code is contributed by Sakshi

Output
2






Time Complexity: O(N),
Auxiliary Space: O(1).


Article Tags :