Tools: 🍫 Chocolate Distribution Problem β€” JavaScript Solution Explained (Sliding Window)

Tools: 🍫 Chocolate Distribution Problem β€” JavaScript Solution Explained (Sliding Window)

Source: Dev.to

🧾 Problem in Simple Words ## 🎯 Goal (Most Important Part) ## πŸ€” Why Difference? ## πŸ”‘ Important Observation ## 🧠 Approach Step-by-Step ## πŸ“¦ Example Walkthrough 1 ## πŸ“¦ Example Walkthrough 2 ## πŸ’» JavaScript Code (Easy) ## ▢️ Test ## πŸ” Code Explanation Line-by-Line ## Sort array ## Store minimum difference ## Create window of size m ## Slide window ## Calculate difference ## Update minimum ## Move window ## ⏱ Time Complexity ## 🧩 Why This Solution is Correct ## 🏁 Final Understanding ## βœ… Final Answer This is a very common coding interview problem that teaches how to distribute items fairly. In this blog, we will understand the problem from zero level and then solve it step-by-step using JavaScript. We have many chocolate packets. Each packet has some chocolates. We also have students: So we must give exactly 1 packet to each student. We must choose m packets such that: is as small as possible. This is called minimized difference. Because we want fair distribution. So we always prefer packets that are close in value. If we sort the packets, similar values come together. Now close chocolate counts are neighbors. So the best m packets will always be next to each other in sorted array. This is the key idea of the problem. Step 1 β†’ Sort the array Step 2 β†’ Take m consecutive packets Step 3 β†’ Find difference Step 4 β†’ Move window Step 5 β†’ Keep minimum This is called sliding window. Now take groups of 3: Now take groups of 3: Now chocolates are in increasing order. Start with very large number. Move window one step each time. So sliding window finds best answer. We want to distribute chocolates fairly. Sorting + sliding window checks all fair groups. Minimum difference is the answer. Chocolate Distribution problem is solved by: Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] CODE_BLOCK: m = number of students Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: m = number of students CODE_BLOCK: m = number of students CODE_BLOCK: m = 3 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: maximum chocolates βˆ’ minimum chocolates Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: maximum chocolates βˆ’ minimum chocolates CODE_BLOCK: maximum chocolates βˆ’ minimum chocolates CODE_BLOCK: [2, 3, 56] diff = 56 βˆ’ 2 = 54 ❌ unfair Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: [2, 3, 56] diff = 56 βˆ’ 2 = 54 ❌ unfair CODE_BLOCK: [2, 3, 56] diff = 56 βˆ’ 2 = 54 ❌ unfair CODE_BLOCK: [2, 3, 4] diff = 4 βˆ’ 2 = 2 βœ… fair Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: [2, 3, 4] diff = 4 βˆ’ 2 = 2 βœ… fair CODE_BLOCK: [2, 3, 4] diff = 4 βˆ’ 2 = 2 βœ… fair CODE_BLOCK: Original: [7, 3, 2, 4, 9, 12, 56] Sorted: [2, 3, 4, 7, 9, 12, 56] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: Original: [7, 3, 2, 4, 9, 12, 56] Sorted: [2, 3, 4, 7, 9, 12, 56] CODE_BLOCK: Original: [7, 3, 2, 4, 9, 12, 56] Sorted: [2, 3, 4, 7, 9, 12, 56] CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] m = 3 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] m = 3 CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] m = 3 CODE_BLOCK: [2, 3, 4, 7, 9, 12, 56] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: [2, 3, 4, 7, 9, 12, 56] CODE_BLOCK: [2, 3, 4, 7, 9, 12, 56] CODE_BLOCK: [2,3,4] β†’ diff = 4βˆ’2 = 2 [3,4,7] β†’ diff = 7βˆ’3 = 4 [4,7,9] β†’ diff = 9βˆ’4 = 5 [7,9,12] β†’ diff = 12βˆ’7 = 5 [9,12,56] β†’ diff = 56βˆ’9 = 47 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: [2,3,4] β†’ diff = 4βˆ’2 = 2 [3,4,7] β†’ diff = 7βˆ’3 = 4 [4,7,9] β†’ diff = 9βˆ’4 = 5 [7,9,12] β†’ diff = 12βˆ’7 = 5 [9,12,56] β†’ diff = 56βˆ’9 = 47 CODE_BLOCK: [2,3,4] β†’ diff = 4βˆ’2 = 2 [3,4,7] β†’ diff = 7βˆ’3 = 4 [4,7,9] β†’ diff = 9βˆ’4 = 5 [7,9,12] β†’ diff = 12βˆ’7 = 5 [9,12,56] β†’ diff = 56βˆ’9 = 47 CODE_BLOCK: arr = [1, 4, 7, 8, 9] m = 3 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: arr = [1, 4, 7, 8, 9] m = 3 CODE_BLOCK: arr = [1, 4, 7, 8, 9] m = 3 CODE_BLOCK: [1, 4, 7, 8, 9] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: [1, 4, 7, 8, 9] CODE_BLOCK: [1, 4, 7, 8, 9] CODE_BLOCK: [1,4,7] β†’ diff = 7βˆ’1 = 6 [4,7,8] β†’ diff = 8βˆ’4 = 4 [7,8,9] β†’ diff = 9βˆ’4 = 2 Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: [1,4,7] β†’ diff = 7βˆ’1 = 6 [4,7,8] β†’ diff = 8βˆ’4 = 4 [7,8,9] β†’ diff = 9βˆ’4 = 2 CODE_BLOCK: [1,4,7] β†’ diff = 7βˆ’1 = 6 [4,7,8] β†’ diff = 8βˆ’4 = 4 [7,8,9] β†’ diff = 9βˆ’4 = 2 COMMAND_BLOCK: function findMinDiff(arr, m) { // Step 1: sort chocolates arr.sort((a, b) => a - b); let minDiff = Number.MAX_SAFE_INTEGER; // window pointers let start = 0; let end = m - 1; // Step 2: slide window while (end < arr.length) { let diff = arr[end] - arr[start]; if (diff < minDiff) { minDiff = diff; } start++; end++; } return minDiff; } Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: function findMinDiff(arr, m) { // Step 1: sort chocolates arr.sort((a, b) => a - b); let minDiff = Number.MAX_SAFE_INTEGER; // window pointers let start = 0; let end = m - 1; // Step 2: slide window while (end < arr.length) { let diff = arr[end] - arr[start]; if (diff < minDiff) { minDiff = diff; } start++; end++; } return minDiff; } COMMAND_BLOCK: function findMinDiff(arr, m) { // Step 1: sort chocolates arr.sort((a, b) => a - b); let minDiff = Number.MAX_SAFE_INTEGER; // window pointers let start = 0; let end = m - 1; // Step 2: slide window while (end < arr.length) { let diff = arr[end] - arr[start]; if (diff < minDiff) { minDiff = diff; } start++; end++; } return minDiff; } CODE_BLOCK: console.log(findMinDiff([7, 3, 2, 4, 9, 12, 56], 3)); Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: console.log(findMinDiff([7, 3, 2, 4, 9, 12, 56], 3)); CODE_BLOCK: console.log(findMinDiff([7, 3, 2, 4, 9, 12, 56], 3)); CODE_BLOCK: 2 Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: arr.sort((a, b) => a - b); Enter fullscreen mode Exit fullscreen mode COMMAND_BLOCK: arr.sort((a, b) => a - b); COMMAND_BLOCK: arr.sort((a, b) => a - b); CODE_BLOCK: let minDiff = Number.MAX_SAFE_INTEGER; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: let minDiff = Number.MAX_SAFE_INTEGER; CODE_BLOCK: let minDiff = Number.MAX_SAFE_INTEGER; CODE_BLOCK: let start = 0; let end = m - 1; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: let start = 0; let end = m - 1; CODE_BLOCK: let start = 0; let end = m - 1; CODE_BLOCK: m = 3 start = 0 end = 2 window = [2,3,4] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: m = 3 start = 0 end = 2 window = [2,3,4] CODE_BLOCK: m = 3 start = 0 end = 2 window = [2,3,4] CODE_BLOCK: while (end < arr.length) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: while (end < arr.length) CODE_BLOCK: while (end < arr.length) CODE_BLOCK: let diff = arr[end] - arr[start]; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: let diff = arr[end] - arr[start]; CODE_BLOCK: let diff = arr[end] - arr[start]; CODE_BLOCK: max = arr[end] min = arr[start] Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: max = arr[end] min = arr[start] CODE_BLOCK: max = arr[end] min = arr[start] CODE_BLOCK: if (diff < minDiff) { minDiff = diff; } Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: if (diff < minDiff) { minDiff = diff; } CODE_BLOCK: if (diff < minDiff) { minDiff = diff; } CODE_BLOCK: start++; end++; Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: start++; end++; CODE_BLOCK: start++; end++; CODE_BLOCK: O(n log n) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: O(n) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: O(n log n) Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: max βˆ’ min β†’ smallest Enter fullscreen mode Exit fullscreen mode CODE_BLOCK: max βˆ’ min β†’ smallest CODE_BLOCK: max βˆ’ min β†’ smallest - Packet 1 β†’ 7 chocolates - Packet 2 β†’ 3 chocolates - Packet 3 β†’ 2 chocolates - Packet 4 β†’ 4 chocolates - Packet 5 β†’ 9 chocolates - Packet 6 β†’ 12 chocolates - Packet 7 β†’ 56 chocolates - closest numbers stay together - best packets must be neighbors - checking neighbors = checking all optimal choices - Sorting packets - Checking all groups of m packets - Choosing group with minimum difference