Capacity To Ship Packages Within D Days
Given an array arr[] consisting of N positive integers representing the weights of N items and a positive integer D, the task is to find the minimum weight capacity of a boat(say K) to ship all weights within D days such that the order of weights loaded on the ship is in the order of the array elements in arr[] and the total amount of weight loaded by ship each day is K.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: arr[] = {1, 2, 1}, D = 2
Output: 3
Explanation:
Consider the minimum weight required by the boat as 3, then below is the order of weights such all the weight can be shipped within D(= 2) days:
Day 1: Ship the weights of values 1 and 2 on the first day as the sum of weights 1 + 2 = 3(<= 3).
Day 2: Ship the weights of value 1 on the second day as the sum of weights 1(<= 3).
Considering the minimum weight amount as 3, ships all the weight within D(= 2) days. Therefore, print 3.Input: arr[] = {9, 8, 10}, D = 3
Output: 10
Approach: The given problem can be solved by using the Greedy Technique and Binary Search. The monotonicity of the problem can be observed that if all packages can be successfully shipped within D days with capacity K, then definitely they can be shipped with any capacity larger than K. Follow the steps below to solve the problem:
- Initialize a variable, say ans as -1 to store the resultant minimum capacity of the boat required.
- Initialize two variables, say s and e with the maximum element in the given array and the total sum of the array respectively which denotes the lower and upper bounds of the search space.
- Iterate until the value of s is less than or equals to e, and perform the following steps:
- Initialize a variable, say mid as (s + e)/2.
- Check if it is possible to ship all the packages within D days when the maximum capacity allowed is mid. If found to be true, then update the value of ans to mid and the value of e to (mid – 1).
- Otherwise, update the value of s to (mid + 1).
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to check if the weights// can be delivered in D days or notbool isValid(int weight[], int n, int D, int mx){ // Stores the count of days required // to ship all the weights if the // maximum capacity is mx int st = 1; int sum = 0; // Traverse all the weights for (int i = 0; i < n; i++) { sum += weight[i]; // If total weight is more than // the maximum capacity if (sum > mx) { st++; sum = weight[i]; } // If days are more than D, // then return false if (st > D) return false; } // Return true for the days < D return true;}// Function to find the least weight// capacity of a boat to ship all the// weights within D daysvoid shipWithinDays(int weight[], int D, int n){ // Stores the total weights to // be shipped int sum = 0; // Find the sum of weights for (int i = 0; i < n; i++) sum += weight[i]; // Stores the maximum weight in the // array that has to be shipped int s = weight[0]; for (int i = 1; i < n; i++) { s = max(s, weight[i]); } // Store the ending value for // the search space int e = sum; // Store the required result int res = -1; // Perform binary search while (s <= e) { // Store the middle value int mid = s + (e - s) / 2; // If mid can be shipped, then // update the result and end // value of the search space if (isValid(weight, n, D, mid)) { res = mid; e = mid - 1; } // Search for minimum value // in the right part else s = mid + 1; } // Print the result cout << res;}// Driver Codeint main(){ int weight[] = { 9, 8, 10 }; int D = 3; int N = sizeof(weight) / sizeof(weight[0]); shipWithinDays(weight, D, N); return 0;} |
Java
// Java program for the above approachimport java.io.*;class GFG{ // Function to check if the weights// can be delivered in D days or notstatic boolean isValid(int[] weight, int n, int D, int mx){ // Stores the count of days required // to ship all the weights if the // maximum capacity is mx int st = 1; int sum = 0; // Traverse all the weights for(int i = 0; i < n; i++) { sum += weight[i]; // If total weight is more than // the maximum capacity if (sum > mx) { st++; sum = weight[i]; } // If days are more than D, // then return false if (st > D) return false; } // Return true for the days < D return true;}// Function to find the least weight// capacity of a boat to ship all the// weights within D daysstatic void shipWithinDays(int[] weight, int D, int n){ // Stores the total weights to // be shipped int sum = 0; // Find the sum of weights for(int i = 0; i < n; i++) sum += weight[i]; // Stores the maximum weight in the // array that has to be shipped int s = weight[0]; for(int i = 1; i < n; i++) { s = Math.max(s, weight[i]); } // Store the ending value for // the search space int e = sum; // Store the required result int res = -1; // Perform binary search while (s <= e) { // Store the middle value int mid = s + (e - s) / 2; // If mid can be shipped, then // update the result and end // value of the search space if (isValid(weight, n, D, mid)) { res = mid; e = mid - 1; } // Search for minimum value // in the right part else s = mid + 1; } // Print the result System.out.println(res);}// Driver Codepublic static void main(String[] args){ int[] weight = { 9, 8, 10 }; int D = 3; int N = weight.length; shipWithinDays(weight, D, N);}}// This code is contributed by Dharanendra L V. |
Python3
# Python3 program for the above approach# Function to check if the weights# can be delivered in D days or notdef isValid(weight, n, D, mx): # Stores the count of days required # to ship all the weights if the # maximum capacity is mx st = 1 sum = 0 # Traverse all the weights for i in range(n): sum += weight[i] # If total weight is more than # the maximum capacity if (sum > mx): st += 1 sum = weight[i] # If days are more than D, # then return false if (st > D): return False # Return true for the days < D return True# Function to find the least weight# capacity of a boat to ship all the# weights within D daysdef shipWithinDays(weight, D, n): # Stores the total weights to # be shipped sum = 0 # Find the sum of weights for i in range(n): sum += weight[i] # Stores the maximum weight in the # array that has to be shipped s = weight[0] for i in range(1, n): s = max(s, weight[i]) # Store the ending value for # the search space e = sum # Store the required result res = -1 # Perform binary search while (s <= e): # Store the middle value mid = s + (e - s) // 2 # If mid can be shipped, then # update the result and end # value of the search space if (isValid(weight, n, D, mid)): res = mid e = mid - 1 # Search for minimum value # in the right part else: s = mid + 1 # Print the result print(res)# Driver Codeif __name__ == '__main__': weight = [ 9, 8, 10 ] D = 3 N = len(weight) shipWithinDays(weight, D, N)# This code is contributed by ipg2016107 |
C#
// C# program for the above approachusing System;class GFG{// Function to check if the weights// can be delivered in D days or notstatic bool isValid(int[] weight, int n, int D, int mx){ // Stores the count of days required // to ship all the weights if the // maximum capacity is mx int st = 1; int sum = 0; // Traverse all the weights for(int i = 0; i < n; i++) { sum += weight[i]; // If total weight is more than // the maximum capacity if (sum > mx) { st++; sum = weight[i]; } // If days are more than D, // then return false if (st > D) return false; } // Return true for the days < D return true;}// Function to find the least weight// capacity of a boat to ship all the// weights within D daysstatic void shipWithinDays(int[] weight, int D, int n){ // Stores the total weights to // be shipped int sum = 0; // Find the sum of weights for(int i = 0; i < n; i++) sum += weight[i]; // Stores the maximum weight in the // array that has to be shipped int s = weight[0]; for(int i = 1; i < n; i++) { s = Math.Max(s, weight[i]); } // Store the ending value for // the search space int e = sum; // Store the required result int res = -1; // Perform binary search while (s <= e) { // Store the middle value int mid = s + (e - s) / 2; // If mid can be shipped, then // update the result and end // value of the search space if (isValid(weight, n, D, mid)) { res = mid; e = mid - 1; } // Search for minimum value // in the right part else s = mid + 1; } // Print the result Console.WriteLine(res);}// Driver Codepublic static void Main(){ int[] weight = { 9, 8, 10 }; int D = 3; int N = weight.Length; shipWithinDays(weight, D, N);}}// This code is contributed by ukasp |
Javascript
<script>// JavaScript program for the above approach// Function to check if the weights// can be delivered in D days or notfunction isValid(weight, n, D, mx){ // Stores the count of days required // to ship all the weights if the // maximum capacity is mx let st = 1; let sum = 0; // Traverse all the weights for(let i = 0; i < n; i++) { sum += weight[i]; // If total weight is more than // the maximum capacity if (sum > mx) { st++; sum = weight[i]; } // If days are more than D, // then return false if (st > D) return false; } // Return true for the days < D return true;} // Function to find the least weight// capacity of a boat to ship all the// weights within D daysfunction shipWithinDays(weight, D, n){ // Stores the total weights to // be shipped let sum = 0; // Find the sum of weights for(let i = 0; i < n; i++) sum += weight[i]; // Stores the maximum weight in the // array that has to be shipped let s = weight[0]; for(let i = 1; i < n; i++) { s = Math.max(s, weight[i]); } // Store the ending value for // the search space let e = sum; // Store the required result let res = -1; // Perform binary search while (s <= e) { // Store the middle value let mid = s + Math.floor((e - s) / 2); // If mid can be shipped, then // update the result and end // value of the search space if (isValid(weight, n, D, mid)) { res = mid; e = mid - 1; } // Search for minimum value // in the right part else s = mid + 1; } // Print the result document.write(res);}// Driver Code let weight = [ 9, 8, 10 ]; let D = 3; let N = weight.length; shipWithinDays(weight, D, N);</script> |
10
Time Complexity: O(N*log(S – M)), where S is the sum of the array elements and M is the maximum element of the array.
Auxiliary Space: O(1)


