Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1120 from Kuljeet-123/chocolate
Chocolate Distribution Problem
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Data Structure/Array Or Vector/Chocolate Distribution Problem/SolutionByKuljeet.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
28 changes: 28 additions & 0 deletions
28
Data Structure/Array Or Vector/Chocolate Distribution Problem/readMe.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` | ||
|