DataTroops Coding Question – Solved

7 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

| Given an n x m grid, where rows are numbered from 7 to n and columns from 1 to … |
| There are 'N' coders standing in a line, where i denotes the ith position of a … |
| A birthday party was attended by N number of kids, and each kid was given a uni… |
| Given a matrix of size m * n, where m denotes the number of rows (starting with… |
| A traveler is traveling from the city of Zeta to Omega. He starts with X amount… |
| As an operations engineer at Amazon, you are responsible for organizing the dis… |