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
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 <bits/stdc++.h> 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<N;i++) maximum=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 += floor(A[j]/i) + 1; } } if(tours<=k) { // Return First Feasible Value found, // since it is also the minimum return i; } } return -1; } //Driver code int main() { int a[]={1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; int n=sizeof(a)/sizeof(a[0]); int k=50; if(getMin(n,a,k)==-1) cout<<"Not Possible"; else cout<<getMin(n,a,k); } //This code is contributed by Mohit kumar 29 |
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<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 += Math.floor(A[j]/i) + 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(String args[]) { int a[]={1, 4, 9, 16, 25, 36, 49, 64, 81, 100}; int n=a.length; int k=50; if(getMin(n,a,k)==-1) System.out.println("Not Possible"); elseSystem.out.println(getMin(n,a,k)); } } //This code is contributed by // Surendra_Gangwar |
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 = 10A = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] k = 50print(getMin(N, A, k)) |
9
Recommended Posts:
- Get maximum items when other items of total cost of an item are free
- Python | Count number of items in a dictionary value that is a list
- Maximum number of groups of size 3 containing two type of items
- Maximum and minimum of an array using minimum number of comparisons
- Find the top K items with the highest value
- Find K items with the lowest values
- How to prevent line breaks in the list of items using CSS?
- Count ways to distribute m items among n people
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- Add minimum number to an array so that the sum becomes even
- Number of subarrays whose minimum and maximum are same
- Form minimum number from given sequence
- Minimum number of 1's to be replaced in a binary array
- Minimum prime number operations to convert A to B
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



