# Minimum number of items to be delivered

• Difficulty Level : Easy
• Last Updated : 25 Nov, 2021

Given N buckets, each containing A[i] items. Given K tours within which all of the items are needed to be delivered. It is allowed to take items from only one bucket in 1 tour. The task is to tell the minimum number of items needed to be delivered per tour so that all of the items can be delivered within K tours.
Conditions : K >= N
Examples

```Input :
N = 5
A[] = { 1, 3, 5, 7, 9 }
K = 10
Output : 3
By delivering 3 items at a time,
Number of tours required for bucket 1 = 1
Number of tours required for bucket 2 = 1
Number of tours required for bucket 3 = 2
Number of tours required for bucket 4 = 3
Number of tours required for bucket 5 = 3
Total number of tours = 10

Input :
N = 10
A = 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
k = 50
Output : 9```

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.

Approach: It is needed to find the minimum number of items per delivery. So, the idea is to iterate from 1 to the maximum value of items in a bucket and calculate the number of tours required for each bucket and find the total number of tours for complete delivery. The first such value with tours less than or equals K gives the required number.
Below is the implementation of the above idea:

## C++

 `//C++ program to find the minimum numbers of tours required``#include ` `using` `namespace` `std;``int` `getMin(``int` `N,``int` `A[],``int` `k)``{``    ``//Iterating through each possible``   ``// value of minimum Items``   ``int` `maximum=0,tours=0;``   ` `   ``for``(``int` `i=0;i

## Java

 `// Java program to find the minimum numbers of tours required``import` `java.util.*;``class` `solution``{` `static` `int` `getMin(``int` `N,``int` `A[],``int` `k)``{``    ``//Iterating through each possible``// value of minimum Items``int` `maximum=``0``,tours=``0``;``    ` `for``(``int` `i=``0``;i

## Python3

 `# Python3 program to find minimum numbers of``# tours required` `def` `getMin(N, A, k):` `    ``# Iterating through each possible``    ``# value of minimum Items``    ``for` `i ``in` `range``(``1``, ``max``(A)``+``1``):``        ``tours ``=` `0``        ``for` `j ``in` `range``(``0``, ``len``(A)):``            ``if``(A[j]``%` `i ``=``=` `0``):``# Perfectly Divisible``                ``tours ``+``=` `A[j]``/``i` `            ``else``:``                ``# Not Perfectly Divisible means required``                ``# tours are Floor Division + 1``                ``tours ``+``=` `A[j]``/``/``i ``+` `1` `        ``if``(tours <``=` `k):``            ``# Return First Feasible Value found,``            ``# since it is also the minimum``            ``return` `i``    ``return` `"Not Possible"` `# Driver Code``N ``=` `10``A ``=` `[``1``, ``4``, ``9``, ``16``, ``25``, ``36``, ``49``, ``64``, ``81``, ``100``]``k ``=` `50``print``(getMin(N, A, k))`

## C#

 `// C# program to find the minimum``// numbers of tours required``using` `System;` `class` `GFG``{` `static` `int` `getMin(``int` `N, ``int` `[] A, ``int` `k)``{``    ``// Iterating through each possible``    ``// value of minimum Items``    ``int` `maximum = 0,tours = 0;``    ` `    ``for``(``int` `i = 0; i < N; i++)``        ``maximum = Math.Max(maximum, A[i]);``        ` `    ``for``(``int` `i = 1; i < maximum + 1; i++)``    ``{``        ``tours = 0;``        ``for``(``int` `j = 0; j < N; j++)``        ``{``            ``if``(A[j] % i == 0)``// perfecctly Divisible``        ``{``            ``tours += A[j] /i;``        ``}``        ``else``        ``{``                ``// Not Perfectly Divisible means required``                ``// tours are Floor Division + 1``                ``tours += (``int``)Math.Floor(A[j] / (i * 1.0)) + 1;``        ``}``    ``}``        ``if``(tours <= k)``        ``{``            ``// Return First Feasible Value found,``            ``// since it is also the minimum``            ``return` `i;``        ``}``    ``}``    ` `    ``return` `-1;``}` `// Driver code``public` `static` `void` `Main()``{``    ``int` `[]a = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};` `    ``int` `n = 10;` `    ``int` `k = 50;` `    ``if``(getMin(n, a, k) == -1)``        ``Console.WriteLine(``"Not Possible"``);``    ``else``        ``Console.WriteLine(getMin(n, a, k));``}``}` `// This code is contributed by``// Mohit kumar`

## PHP

 ``

## Javascript

 ``
Output
`9`

Time Complexity: O(N * MAX) where N is the total number of elements in the array and MAX is the maximum element in the array.
Auxiliary Space: O(1)

Efficient Approach:

The maximum number of items that can be delivered per tour is the maximum element in the array. With this information we can use binary search  where initially low = 1 and high =  maximum element + 1 and find the number of tours required when number of items needed to be delivered per tour is mid where mid =  low + (high – low)/2. If it less than or equal to k, then we update answer to  mid  and set high = mid, otherwise update low to mid.

Below is the implementation of the above approach:

## C++

 `//C++ program to find the minimum numbers of tours required` `#include``using` `namespace` `std;` `int` `reqdTours(vector<``int``> a,``int` `cur)``{``    ``int` `cur_tours = 0;``     ``int` `n=(``int``)a.size();``    ``for``(``int` `i=0; i< n;i++ )``        ``cur_tours += ( a[i]+cur-1)/cur;``    ``return` `cur_tours;``}` `int` `getMin(vector<``int``>a , ``int` `k)``{``    ``int` `maxm=0;``    ``int` `n=(``int``)a.size();``    ` `    ``// find the maximum element in array``    ``for``(``int` `i=0;i a={1, 4, 9, 16, 25, 36, 49, 64, 81, 100};``    ``int` `k=50;` `    ``if``(getMin(a,k)==-1)``    ``cout<<``"Not Possible"``;``    ``else``    ``cout<
Output
`9`

Time Complexity: O(N * log(MAX)) where N is the total number of elements in the array and MAX is the maximum element in the array.
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up