Skip to content

Commit 0e9c209

Browse files
committed
Chocolate Distribution Problem
1 parent 4db27e5 commit 0e9c209

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [Array Subset of another array](https://practice.geeksforgeeks.org/problems/array-subset-of-another-array/0 "view question") - [Cpp Solution](./solutions/Array%20Subset%20of%20another%20array.cpp)
3131
- [Triplet Sum in Array](https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1# "view question") - [Cpp Solution](./solutions/Triplet%20Sum%20in%20Array.cpp)
3232
- [Trapping Rain Water](https://practice.geeksforgeeks.org/problems/trapping-rain-water-1587115621/1# "view question") - [Cpp Solution](./solutions/Trapping%20Rain%20Water.cpp)
33+
- [Chocolate Distribution Problem](https://practice.geeksforgeeks.org/problems/chocolate-distribution-problem/0# "view question") - [Cpp Solution](./solutions/Chocolate%20Distribution%20Problem.cpp)
3334
- []( "view question") - [Cpp Solution](./solutions/.cpp)
3435

3536
<!-- - []( "view question") - [Cpp Solution](./solutions/) -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Chocolate Distribution Problem
3+
==============================
4+
5+
Given an array A of positive integers of size N, 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 :
6+
1. Each student gets one packet.
7+
2. The difference between the number of chocolates given to the students having packet with maximum chocolates and student having packet with minimum chocolates is minimum.
8+
9+
Input:
10+
The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. The first line of each test case contains an integer N denoting the number of packets. Then next line contains N space separated values of the array A denoting the values of each packet. The third line of each test case contains an integer m denoting the no of students.
11+
12+
Output:
13+
For each test case in a new line print the minimum difference.
14+
15+
Constraints:
16+
1 <= T <= 100
17+
1 <=N<= 107
18+
1 <= Ai <= 1018
19+
1 <= M <= N
20+
21+
Example:
22+
Input:
23+
2
24+
8
25+
3 4 1 9 56 7 9 12
26+
5
27+
7
28+
7 3 2 4 9 12 56
29+
3
30+
31+
Output:
32+
6
33+
2
34+
35+
Explanation:
36+
Testcase 1: The minimum difference between maximum chocolates and minimum chocolates is 9-3=6
37+
*/
38+
39+
#include <bits/stdc++.h>
40+
using namespace std;
41+
42+
int main()
43+
{
44+
int t;
45+
cin >> t;
46+
while (t--)
47+
{
48+
int n;
49+
cin >> n;
50+
vector<int> arr(n, 0);
51+
for (auto &i : arr)
52+
cin >> i;
53+
int m;
54+
cin >> m;
55+
56+
sort(arr.begin(), arr.end());
57+
int ans = arr[m - 1] - arr[0];
58+
59+
int i = 1, j = m;
60+
while (j < n)
61+
{
62+
ans = min(ans, arr[j] - arr[i]);
63+
i++;
64+
j++;
65+
}
66+
cout << ans << endl;
67+
}
68+
return 0;
69+
}

0 commit comments

Comments
 (0)