|
| 1 | +<h1 align="center">Chocolate - Distribution - Problem</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Chocolate Distribution Problem](https://www.geeksforgeeks.org/problems/chocolate-distribution-problem3825/1) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +### Problem Explanation |
| 10 | +We are given `n` packets of chocolates, each packet containing a certain number of chocolates. We are also given `m` students, and our task is to distribute the packets such that the difference between the largest and smallest number of chocolates distributed to the students is minimized. The problem is essentially about finding a subset of `m` packets such that the difference between the maximum and minimum chocolates in that subset is as small as possible. |
| 11 | + |
| 12 | +#### Example: |
| 13 | + |
| 14 | +**Input**: |
| 15 | +- `n = 8` (the number of chocolate packets) |
| 16 | +- `m = 5` (the number of students to distribute chocolates) |
| 17 | +- `chocolates = [12, 4, 7, 9, 2, 23, 25, 41]` |
| 18 | + |
| 19 | +**Goal**: Select `m` packets such that the difference between the maximum and minimum number of chocolates in any selected subset of `m` packets is minimized. |
| 20 | + |
| 21 | +**Steps**: |
| 22 | +1. **Sort the Array**: First, sort the array of chocolates. Sorting brings packets with similar chocolate counts next to each other. |
| 23 | +2. **Sliding Window**: Consider a sliding window of size `m`. Move this window over the sorted array and calculate the difference between the maximum and minimum chocolates in each window. |
| 24 | +3. **Track the Minimum Difference**: For each window, calculate the difference between the maximum and minimum chocolates, and track the smallest difference encountered. |
| 25 | + |
| 26 | +#### Solution: |
| 27 | + |
| 28 | +- **Sort the array**: `[2, 4, 7, 9, 12, 23, 25, 41]` |
| 29 | +- Now, for every subset of `m = 5` packets, compute the difference between the maximum and minimum number of chocolates. |
| 30 | + |
| 31 | +#### Sliding Window Examples: |
| 32 | +1. Window 1: `[2, 4, 7, 9, 12]` → Difference = `12 - 2 = 10` |
| 33 | +2. Window 2: `[4, 7, 9, 12, 23]` → Difference = `23 - 4 = 19` |
| 34 | +3. Window 3: `[7, 9, 12, 23, 25]` → Difference = `25 - 7 = 18` |
| 35 | +4. Window 4: `[9, 12, 23, 25, 28]` → Difference = `28 - 9 = 19` |
| 36 | +5. Window 5: `[12, 23, 25, 28, 30]` → Difference = `30 - 12 = 18` |
| 37 | +6. Window 6: `[23, 25, 28, 30, 40]` → Difference = `40 - 23 = 17` |
| 38 | +7. Window 7: `[25, 28, 30, 40, 40]` → Difference = `40 - 25 = 15` |
| 39 | +8. Window 8: `[28, 30, 40, 40, 41]` → Difference = `41 - 28 = 13` |
| 40 | + |
| 41 | +**Minimum Difference**: The minimum difference is `10`, which occurs in the first window `[2, 4, 7, 9, 12]`. |
| 42 | + |
| 43 | + |
| 44 | +### Greedy Algorithm Approach (Step-by-Step): |
| 45 | + |
| 46 | +The **Greedy Algorithm** works in the following steps to find the solution for the Chocolate Distribution Problem: |
| 47 | + |
| 48 | +1. **Sort the Array**: The first step is to sort the array of chocolates in non-decreasing order. This allows us to easily consider the packets that are closer in value. |
| 49 | + |
| 50 | +2. **Consider All Possible Windows**: After sorting, you need to consider all possible contiguous subarrays (or windows) of size `m`. For each window, calculate the difference between the largest and smallest values. |
| 51 | + |
| 52 | +3. **Track the Minimum Difference**: As you move through each window, keep track of the minimum difference between the maximum and minimum values in any window. |
| 53 | + |
| 54 | +4. **Return the Minimum Difference**: Once all possible windows are checked, the smallest difference found is the answer. |
| 55 | + |
| 56 | +#### Example: |
| 57 | + |
| 58 | +- **Input**: `chocolates = [12, 4, 7, 9, 2, 23, 25, 41]`, `m = 5` |
| 59 | +- **Sorted Array**: `[2, 4, 7, 9, 12, 23, 25, 41]` |
| 60 | +- **Sliding Window Size**: `m = 5` |
| 61 | + |
| 62 | +Iterate through the sorted array and check all possible windows of size 5: |
| 63 | +1. `[2, 4, 7, 9, 12]`: Max - Min = `12 - 2 = 10` |
| 64 | +2. `[4, 7, 9, 12, 23]`: Max - Min = `23 - 4 = 19` |
| 65 | +3. `[7, 9, 12, 23, 25]`: Max - Min = `25 - 7 = 18` |
| 66 | +4. `[9, 12, 23, 25, 28]`: Max - Min = `28 - 9 = 19` |
| 67 | +5. `[12, 23, 25, 28, 30]`: Max - Min = `30 - 12 = 18` |
| 68 | +6. `[23, 25, 28, 30, 40]`: Max - Min = `40 - 23 = 17` |
| 69 | +7. `[25, 28, 30, 40, 40]`: Max - Min = `40 - 25 = 15` |
| 70 | +8. `[28, 30, 40, 40, 41]`: Max - Min = `41 - 28 = 13` |
| 71 | + |
| 72 | +The **minimum difference** is `10` from the first window. |
| 73 | + |
| 74 | +## Problem Solution |
| 75 | +```cpp |
| 76 | +class Solution { |
| 77 | + public: |
| 78 | + // Function to find the minimum difference between the maximum and minimum values of m selected elements |
| 79 | + int findMinDiff(vector<int>& a, int m) { |
| 80 | + |
| 81 | + // Step 1: Sort the array to bring the closest values together |
| 82 | + sort(a.begin(), a.end()); |
| 83 | + |
| 84 | + // Step 2: Initialize two pointers, i and j, to represent the window of m elements |
| 85 | + int i = 0; |
| 86 | + int j = m - 1; |
| 87 | + |
| 88 | + // Step 3: Initialize the variable 'mini' to store the minimum difference found |
| 89 | + int mini = INT_MAX; |
| 90 | + |
| 91 | + // Step 4: Iterate through the array using the sliding window approach |
| 92 | + // The window size is m elements |
| 93 | + while (j < a.size()) { |
| 94 | + // Calculate the difference between the maximum and minimum values in the current window (a[j] and a[i]) |
| 95 | + int diff = a[j] - a[i]; |
| 96 | + |
| 97 | + // Step 5: Update 'mini' with the smaller value between the current minimum difference and the calculated difference |
| 98 | + mini = min(mini, diff); |
| 99 | + |
| 100 | + // Move the window: increment both i and j to check the next possible set of m elements |
| 101 | + i++; |
| 102 | + j++; |
| 103 | + } |
| 104 | + |
| 105 | + // Step 6: Return the minimum difference found |
| 106 | + return mini; |
| 107 | + } |
| 108 | +}; |
| 109 | +``` |
| 110 | +
|
| 111 | +## Problem Solution Explanation |
| 112 | +
|
| 113 | +1. **Class Declaration**: |
| 114 | + ```cpp |
| 115 | + class Solution { |
| 116 | + ``` |
| 117 | + - This line defines the class `Solution`, which contains the solution to the problem. The class will hold the function `findMinDiff`, which is the core function for solving the problem. |
| 118 | + |
| 119 | +2. **Function Declaration**: |
| 120 | + ```cpp |
| 121 | + int findMinDiff(vector<int>& a, int m) { |
| 122 | + ``` |
| 123 | + - This line declares the function `findMinDiff` that takes two parameters: |
| 124 | + - `a`: A vector of integers representing the chocolate packet sizes. |
| 125 | + - `m`: The number of students (or the number of packets to distribute). |
| 126 | + - The function returns an integer, which is the minimum difference between the largest and smallest number of chocolates among `m` selected packets. |
| 127 | +
|
| 128 | +3. **Sorting the Array**: |
| 129 | + ```cpp |
| 130 | + sort(a.begin(), a.end()); |
| 131 | + ``` |
| 132 | + - **Explanation**: The array `a` is sorted in non-decreasing order. |
| 133 | + - **Why?**: Sorting helps in easily finding the closest chocolate packet counts, as the solution requires selecting packets that minimize the difference between the maximum and minimum values in a subset. |
| 134 | + - **Example**: If `a = [12, 4, 7, 9, 2, 23, 25, 41]`, after sorting, it becomes `a = [2, 4, 7, 9, 12, 23, 25, 41]`. |
| 135 | + |
| 136 | +4. **Initializing Variables `i`, `j`, and `mini`**: |
| 137 | + ```cpp |
| 138 | + int i = 0; |
| 139 | + int j = m - 1; |
| 140 | + int mini = INT_MAX; |
| 141 | + ``` |
| 142 | + - **Explanation**: |
| 143 | + - `i`: Represents the starting index of the sliding window of size `m`. |
| 144 | + - `j`: Represents the ending index of the sliding window. |
| 145 | + - `mini`: This variable will store the smallest difference found between the maximum and minimum values in the window of size `m`. |
| 146 | + - **Example**: If `m = 5`, `i = 0` and `j = 4` initially. |
| 147 | + |
| 148 | +5. **Sliding Window Loop**: |
| 149 | + ```cpp |
| 150 | + while (j < a.size()) { |
| 151 | + ``` |
| 152 | + - **Explanation**: The `while` loop iterates as long as `j` is less than the size of the array `a`. |
| 153 | + - The loop represents the sliding window mechanism: for each iteration, we evaluate a window of `m` packets starting from `i` and ending at `j`. |
| 154 | +
|
| 155 | +6. **Calculate the Difference**: |
| 156 | + ```cpp |
| 157 | + int diff = a[j] - a[i]; |
| 158 | + ``` |
| 159 | + - **Explanation**: In this line, we calculate the difference between the maximum and minimum values in the current window (the values at indices `i` and `j`). |
| 160 | + - **Why?**: We want to minimize the difference between the largest and smallest chocolates within a selected set of packets. |
| 161 | + - **Example**: For the window `[2, 4, 7, 9, 12]`, `diff = 12 - 2 = 10`. |
| 162 | + |
| 163 | +7. **Update the Minimum Difference**: |
| 164 | + ```cpp |
| 165 | + mini = min(mini, diff); |
| 166 | + ``` |
| 167 | + - **Explanation**: We update `mini` with the minimum value between the current `mini` (which stores the smallest difference found so far) and the current difference `diff`. |
| 168 | + - **Why?**: This ensures that `mini` always contains the smallest difference encountered in any of the sliding windows. |
| 169 | + - **Example**: Initially, `mini = INT_MAX`. After the first window, `mini = 10`. |
| 170 | + |
| 171 | +8. **Move the Window**: |
| 172 | + ```cpp |
| 173 | + i++; |
| 174 | + j++; |
| 175 | + ``` |
| 176 | + - **Explanation**: Both `i` and `j` are incremented to move the window one step forward. |
| 177 | + - **Why?**: This shifts the window by one position, allowing us to consider the next possible set of `m` elements. |
| 178 | + - **Example**: For the window `[2, 4, 7, 9, 12]`, after incrementing, `i = 1` and `j = 5`. |
| 179 | + |
| 180 | +9. **Return the Minimum Difference**: |
| 181 | + ```cpp |
| 182 | + return mini; |
| 183 | + ``` |
| 184 | + - **Explanation**: Once the loop has processed all possible windows, the function returns the value of `mini`, which holds the minimum difference between the largest and smallest chocolates in any selected subset of `m` packets. |
| 185 | + - **Example**: After considering all windows, `mini = 10` will be returned. |
| 186 | + |
| 187 | + |
| 188 | +### Example Walkthrough: |
| 189 | + |
| 190 | +For the input: |
| 191 | +- `chocolates = [12, 4, 7, 9, 2, 23, 25, 41]` |
| 192 | +- `m = 5` |
| 193 | + |
| 194 | +After sorting: |
| 195 | +- `a = [2, 4, 7, 9, 12, 23, 25, 41]` |
| 196 | + |
| 197 | +#### Iteration Details: |
| 198 | +1. **First Window** (`i = 0, j = 4`): `[2, 4, 7, 9, 12]` → Difference = `12 - 2 = 10` |
| 199 | +2. **Second Window** (`i = 1, j = 5`): `[4, 7, 9, 12, 23]` → Difference = `23 - 4 = 19` |
| 200 | +3. **Third Window** (`i = 2, j = 6`): `[7, 9, 12, 23, 25]` → Difference = `25 - 7 = 18` |
| 201 | +4. **Fourth Window** (`i = 3, j = 7`): `[9, 12, 23, 25, 41]` → Difference = `41 - 9 = 32` |
| 202 | + |
| 203 | +The minimum difference is `10`, which occurs in the first window. |
| 204 | + |
| 205 | + |
| 206 | +### Time and Space Complexity: |
| 207 | + |
| 208 | +#### Time Complexity: |
| 209 | +- **Sorting the Array**: `O(n log n)` where `n` is the number of chocolate packets. This is because the sorting step takes `O(n log n)` time. |
| 210 | +- **Sliding Window Calculation**: `O(n)` because we only need to iterate through the array once to calculate the differences and update the minimum difference. |
| 211 | + |
| 212 | +**Overall Time Complexity**: `O(n log n)` because the sorting dominates the overall complexity. |
| 213 | + |
| 214 | +#### Space Complexity: |
| 215 | +- **Auxiliary Space**: The algorithm uses only a constant amount of extra space, regardless of the size of the input (other than the input array itself). |
| 216 | + |
| 217 | +**Overall Space Complexity**: `O(1)` (constant space). |
| 218 | + |
| 219 | +In summary, the time complexity of this solution is `O(n log n)` due to the sorting step, and the space complexity is `O(1)` as we use only a fixed amount of extra space. |
| 220 | + |
| 221 | + |
| 222 | + |
0 commit comments