DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

๐Ÿซ Chocolate Distribution Problem โ€” JavaScript Solution Explained (Sliding Window)

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.


๐Ÿงพ Problem in Simple Words

We have many chocolate packets.

Each packet has some chocolates.

Example:

arr = [7, 3, 2, 4, 9, 12, 56]
Enter fullscreen mode Exit fullscreen mode

This means:

  • 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

We also have students:

m = number of students
Enter fullscreen mode Exit fullscreen mode

Example:

m = 3
Enter fullscreen mode Exit fullscreen mode

So we must give exactly 1 packet to each student.


๐ŸŽฏ Goal (Most Important Part)

We must choose m packets such that:

maximum chocolates โˆ’ minimum chocolates
Enter fullscreen mode Exit fullscreen mode

is as small as possible.

This is called minimized difference.


๐Ÿค” Why Difference?

Because we want fair distribution.

Bad distribution:

[2, 3, 56]
diff = 56 โˆ’ 2 = 54 โŒ unfair
Enter fullscreen mode Exit fullscreen mode

Good distribution:

[2, 3, 4]
diff = 4 โˆ’ 2 = 2 โœ… fair
Enter fullscreen mode Exit fullscreen mode

So we always prefer packets that are close in value.


๐Ÿ”‘ Important Observation

If we sort the packets, similar values come together.

Example:

Original: [7, 3, 2, 4, 9, 12, 56]
Sorted:   [2, 3, 4, 7, 9, 12, 56]
Enter fullscreen mode Exit fullscreen mode

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.


๐Ÿง  Approach Step-by-Step

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.


๐Ÿ“ฆ Example Walkthrough 1


arr = [7, 3, 2, 4, 9, 12, 56]
m = 3
Enter fullscreen mode Exit fullscreen mode

Sort:

[2, 3, 4, 7, 9, 12, 56]
Enter fullscreen mode Exit fullscreen mode

Now take groups of 3:

[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

Minimum = 2

So answer = 2


๐Ÿ“ฆ Example Walkthrough 2

arr = [1, 4, 7, 8, 9]
m = 3
Enter fullscreen mode Exit fullscreen mode

Sort:

[1, 4, 7, 8, 9]
Enter fullscreen mode Exit fullscreen mode

Now take groups of 3:

[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

Minimum = 2

So answer = 2

๐Ÿ’ป JavaScript Code (Easy)

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

โ–ถ๏ธ Test

console.log(findMinDiff([7, 3, 2, 4, 9, 12, 56], 3));
Enter fullscreen mode Exit fullscreen mode

Output:

2
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Code Explanation Line-by-Line

Sort array

arr.sort((a, b) => a - b);
Enter fullscreen mode Exit fullscreen mode

Now chocolates are in increasing order.


Store minimum difference

let minDiff = Number.MAX_SAFE_INTEGER;
Enter fullscreen mode Exit fullscreen mode

Start with very large number.


Create window of size m

let start = 0;
let end = m - 1;
Enter fullscreen mode Exit fullscreen mode

Example:

m = 3
start = 0
end = 2
window = [2,3,4]
Enter fullscreen mode Exit fullscreen mode

Slide window

while (end < arr.length)
Enter fullscreen mode Exit fullscreen mode

Move window one step each time.


Calculate difference

let diff = arr[end] - arr[start];
Enter fullscreen mode Exit fullscreen mode

Because:

max = arr[end]
min = arr[start]
Enter fullscreen mode Exit fullscreen mode

Update minimum

if (diff < minDiff) {
  minDiff = diff;
}
Enter fullscreen mode Exit fullscreen mode

Keep best answer.


Move window

start++;
end++;
Enter fullscreen mode Exit fullscreen mode

Next group.


โฑ Time Complexity

Sorting:

O(n log n)
Enter fullscreen mode Exit fullscreen mode

Sliding window:

O(n)
Enter fullscreen mode Exit fullscreen mode

Total:

O(n log n)
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฉ Why This Solution is Correct

After sorting:

  • closest numbers stay together
  • best packets must be neighbors
  • checking neighbors = checking all optimal choices

So sliding window finds best answer.


๐Ÿ Final Understanding

We want to distribute chocolates fairly.

Fair means:

max โˆ’ min โ†’ smallest
Enter fullscreen mode Exit fullscreen mode

Sorting + sliding window checks all fair groups.

Minimum difference is the answer.


โœ… Final Answer

Chocolate Distribution problem is solved by:

  • Sorting packets
  • Checking all groups of m packets
  • Choosing group with minimum difference

Top comments (0)