DataTroops Coding Question – Solved

12 Live
Problem Statement You are given an array arr of positive integers of size n. Each value in the array represents the number of toffees in a packet. Each packet can have any number of toffees. For example, if arr = [2, 3], this means that the first packet has 2 toffees and the second has 3 toffees. There are x students. The task is to distribute toffee packets among x students such that: - Each student gets exactly one packet. - The difference between the maximum number of toffees given to a student and the minimum number of toffees given to a student is minimized. Input & Output Input Format: - The first line is an integer n, which represents the number of chocolate packets. - The next line is an integer x, which represents the number of students. - The next line contains n space-separated integer values that represent the number of toffees in each packet. Input Constraints: - n > x > 1 Output Format: - An integer representing the difference between the maximum and minimum number of toffees given to a student. Example 1: Input: 7 3 7 3 2 4 9 12 56 Output: 2 Explanation: Imagine Student 1 got the 2nd packet, Student 2 got the 3rd packet, and Student 3 got the 4th packet. So, the three students get 3, 2, and 4 toffees respectively. The difference between the maximum (4) and minimum (2) number of toffees is 2. For no other distribution will this difference be smaller.

Asked in: DataTroops

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |
| Undirected Coloured Graph Shortest Path You are given an undirected weight… |
| Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to… |
| Academic Decathlon Students are being selected for an academic decathlon tea… |
| Sum of Arrays Given two arrays each of length n, arr1 and arr2, in one opera… |
| Count Swaps During Custom Sorting Analyze the efficiency of the following so… |