From 3bdbd55968e022e249bcfc5958076790733a9753 Mon Sep 17 00:00:00 2001 From: Kuljeet-123 Date: Sat, 25 Jul 2020 21:28:02 +0530 Subject: [PATCH] Chocolate Distribution Problem --- .../SolutionByKuljeet.cpp | 29 +++++++++++++++++++ .../Chocolate Distribution Problem/readMe.md | 28 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 Data Structure/Array Or Vector/Chocolate Distribution Problem/SolutionByKuljeet.cpp create mode 100644 Data Structure/Array Or Vector/Chocolate Distribution Problem/readMe.md diff --git a/Data Structure/Array Or Vector/Chocolate Distribution Problem/SolutionByKuljeet.cpp b/Data Structure/Array Or Vector/Chocolate Distribution Problem/SolutionByKuljeet.cpp new file mode 100644 index 000000000..e6a87bac5 --- /dev/null +++ b/Data Structure/Array Or Vector/Chocolate Distribution Problem/SolutionByKuljeet.cpp @@ -0,0 +1,29 @@ +#include +#include +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; +} \ No newline at end of file diff --git a/Data Structure/Array Or Vector/Chocolate Distribution Problem/readMe.md b/Data Structure/Array Or Vector/Chocolate Distribution Problem/readMe.md new file mode 100644 index 000000000..44f75dbb9 --- /dev/null +++ b/Data Structure/Array Or Vector/Chocolate Distribution Problem/readMe.md @@ -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 + +``` +