Tools
Tools: ๐ซ Chocolate Distribution Problem โ JavaScript Solution Explained (Sliding Window)
2026-02-16
0 views
admin
๐งพ 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 ? It will become hidden in your post, but will still be visible via the comment's permalink. as well , this person and/or CODE_BLOCK: arr = [7, 3, 2, 4, 9, 12, 56] 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 CODE_BLOCK: m = number of students CODE_BLOCK: m = number of students CODE_BLOCK: m = 3 CODE_BLOCK: maximum chocolates โ minimum chocolates CODE_BLOCK: maximum chocolates โ minimum chocolates CODE_BLOCK: maximum chocolates โ minimum chocolates CODE_BLOCK: [2, 3, 56] diff = 56 โ 2 = 54 โ unfair 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 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] 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 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] 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 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 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] 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 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; } 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)); 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 COMMAND_BLOCK: arr.sort((a, b) => a - b); COMMAND_BLOCK: arr.sort((a, b) => a - b); COMMAND_BLOCK: arr.sort((a, b) => a - b); CODE_BLOCK: let minDiff = Number.MAX_SAFE_INTEGER; 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; 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] 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) CODE_BLOCK: while (end < arr.length) CODE_BLOCK: while (end < arr.length) CODE_BLOCK: let diff = arr[end] - arr[start]; CODE_BLOCK: let diff = arr[end] - arr[start]; CODE_BLOCK: let diff = arr[end] - arr[start]; CODE_BLOCK: max = arr[end] min = arr[start] CODE_BLOCK: max = arr[end] min = arr[start] CODE_BLOCK: max = arr[end] min = arr[start] CODE_BLOCK: if (diff < minDiff) { minDiff = diff; } CODE_BLOCK: if (diff < minDiff) { minDiff = diff; } CODE_BLOCK: if (diff < minDiff) { minDiff = diff; } CODE_BLOCK: start++; end++; CODE_BLOCK: start++; end++; CODE_BLOCK: start++; end++; CODE_BLOCK: O(n log n) CODE_BLOCK: O(n) CODE_BLOCK: O(n log n) CODE_BLOCK: max โ min โ smallest 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
toolsutilitiessecurity toolschocolatedistributionproblemjavascriptsolutionexplainedsliding