Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6560 |

@blackshadows | 6400 |

@mb1973 | 5777 |

@Quandray | 5271 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5074 |

@kirtidee18 | 4356 |

@sanjay05 | 3762 |

@mantu_singh | 3638 |

@gfgaccount | 3601 |

@marius_valentin_dragoi | 3525 |

Complete Leaderboard | |

Handle | Score |

@anish5256 | 991 |

@vimleshpratapsingh321 | 980 |

@skj7 | 871 |

@akhileshkumar562002 | 862 |

@pradeeppatidar1999 | 840 |

@kanewilliamson123 | 837 |

@pd420786 | 813 |

@infilooop | 800 |

@bunnybug320 | 796 |

@smitabose7826 | 784 |

@virgat | 768 |

Complete Leaderboard |

Given an array **A[ ]** of positive integers of size **N**, where each value represents the number of chocolates in a packet. Each packet can have a variable number of chocolates. There are **M** students, the task is to distribute chocolate packets among **M** students such that :

1. Each student gets **exactly** one packet.

2. The difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student is minimum.

**Example 1:**

Input:N = 8, M = 5 A = {3, 4, 1, 9, 56, 7, 9, 12}Output:6Explanation:The minimum difference between maximum chocolates and minimum chocolates is 9 - 3 = 6 by choosing following M packets : {3, 4, 9, 7, 9}.

**Example 2:**

Input:N = 7, M = 3 A = {7, 3, 2, 4, 9, 12, 56}Output:2Explanation:The minimum difference between maximum chocolates and minimum chocolates is 4 - 2 = 2 by choosing following M packets : {3, 2, 4}.

**Your Task:**

You don't need to take any input or print anything. Your task is to complete the function **findMinDiff() **which takes array A[ ], N and M as input parameters and returns the minimum possible difference between maximum number of chocolates given to a student and minimum number of chocolates given to a student.

**Expected Time Complexity: **O(N*Log(N))

**Expected Auxiliary Space: **O(1)

**Constraints:**

1 ≤ T ≤ 100

1 ≤ N ≤ 10^{5}

1 ≤ A_{i} ≤ 10^{9}

1 ≤ M ≤ N

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Chocolate Distribution Problem

...