Update appNew update is available. Click here to update.

Chocolates With Magical Number

Contributed by
Amit Priyankar
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 10 mins
Success Rate 90 %
Share
3 upvotes

Problem Statement

Ninja is planning to give ‘PACKET’ of 'N' chocolates to his friend on his birthday. Each chocolate in ‘PACKET’ has a non-negative sweetness level that denotes how sweet the chocolate is. He wants to give these chocolates to his friend such that the absolute difference between chocolate with maximum sweetness and minimum sweetness should be as minimum as possible.

In order to do this, Ninja uses a magic number, 'K' under the following conditions:

1. Using this magical number, Ninja can either increase or decrease the sweetness of each chocolate. 
2. After increasing or decreasing the sweetness, all the new sweetness values must be non-negative. 
3. Ninja must use this magic number on each chocolate exactly once.

For Example :

For ‘PACKETS’ = [1, 2, 3, 4, 5] and 'K' =  1, the absolute difference between two chocolates with maximum (5) and minimum (1) sweetness is 4. Now in order to minimize this value, Ninja increases [1, 2, 3] and decreases [4, 5] by 1 (‘K’ = 1). So, ‘PACKET’ becomes [2,3,4,3,4]. Now, the absolute difference between the two chocolates with maximum (4) and minimum (2) sweetness is 2 which is the minimum possible. 

As Ninja is busy preparing for the party, he asks you for help. Can you help Ninja determine the minimum possible difference between the maximum and minimum sweetness of chocolates in 'PACKET’ after using the magic number 'K'?

Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 100
1 <= N <= 10^5
1 <= K <= 10^5
1 <= PACKET[i] <= 10^5

Where 'T' is the number of test cases, 'N' denotes the number of chocolates in the given array/list 'PACKET' and 'K' denotes the given magic number, respectively. 'PACKET[i]' denotes the sweetness of the i'th chocolate. 

Time Limit : 1 sec 
Sample Input 1 :
3
2 1
5 2 
1 2
3
4 3
5 5 5 5
Sample Output 1 :
1
0
0
Explanation of Sample Input 1 :
For the first test case:
Given ‘K’ = 1 so, [6, 3], [6, 1], [4, 1] and [4, 3] are all the possible ways in which sweetness of chocolates can be increased or decreased among which [4,3] has the least difference of 1 between the minimum and maximum sweetness value.   

For the second test case :
Given ‘K’ = 2 so [5] and [1] are the only possible ways in which the sweetness of chocolates can be increased or decreased and the minimum difference between the minimum and maximum sweetness value obtained in both ways is 0.   

For the third test case:
Given ‘K’ = 3 and all the elements of ‘PACKET’ are the same. Among all possible ways of increasing or decreasing sweetness values of chocolates by ‘K’, [8, 8, 8, 8] and [2, 2, 2, 2] are the only possible ways which yield a minimum difference of 0 in the maximum and minimum sweetness values.    
Sample Input 2 :
3
2 10
1 2
4 8
1 2 3 4
3 2
2 4 6
Sample Output 2 :
1
3
2
Explanation of Sample Input 2 :
For the first test case:
Given ‘K’ = 10 so we can only increase the sweetness of all chocolates in ‘PACKET’ by ‘K’ because the sweetness of chocolates must be positive. We get [11,12] after modifying the ‘PACKET’. So, the least difference in the maximum and minimum sweetness values is 1. 

For the second test case:
Given ‘K’ = 8 so we can only increase the sweetness of all chocolates in ‘PACKET’ by ‘K’ because the sweetness of chocolates must be positive. We get [9,10,11,12] after modifying the ‘PACKET’. So, the least difference in the maximum and minimum sweetness values is 3.    

For the third test case :
Among all possible ways of increasing or decreasing the sweetness of chocolates in the given ‘PACKET’ by ‘K’, [4,2,4] and [4,6,4] are the optimal chocolate sweetness obtained which have the least difference of 2 between the maximum and the minimum sweetness values among all possibilities.
Reset Code
Full screen
Auto
copy-code
Console