Skip to content

Commit

Permalink
Merge pull request #1120 from Kuljeet-123/chocolate
Browse files Browse the repository at this point in the history
Chocolate Distribution Problem
  • Loading branch information
Shoaib Rayeen committed Jul 31, 2020
2 parents 67512e2 + 3bdbd55 commit d9a304f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
@@ -0,0 +1,29 @@
#include <iostream>
#include <algorithm>
using namespace std;

int minDiff(int arr[], int n, int m)
{
if(m > n)
return -1;

sort(arr,arr+n);
int res = arr[m-1] - arr[0];
for(int i = 1;(i+m-1) < n;i++)
res = min(res,(arr[i+m-1]-arr[i]));

return res;
}

int main()
{
int n;
cin >> n;
int arr[n];
for(int i = 0;i < n;i++)
cin >> arr[i];
int m;
cin >> m;
cout << minDiff(arr,n,m);
return 0;
}
@@ -0,0 +1,28 @@
# Given an array of n integers where each value represents number of chocolates in a packet. Each packet can have variable number of chocolates. There are m students, the task is to distribute chocolate packets such that:

##### 1.Each student gets one packet.
##### 2.The difference between the number of chocolates in packet with maximum chocolates and packet with minimum chocolates given to the students is minimum.

# Constraints
- 1 < size Of Array <= 10^6
- 0 < Array Elements <= 10^9

# Input
- Array
- Its Size
- No. of childrens

# Output
- Print the Minimum difference between the m packets

```
Input : arr[] = {7, 3, 2, 4, 9, 12, 56}
m = 3
Output: 2
Input : arr[] = {3, 4, 1, 9, 56, 7, 9, 12}
m = 5
Output: 6
```

0 comments on commit d9a304f

Please sign in to comment.