#C9723. Minimizing the Beauty of an Array

    ID: 53848 Type: Default 1000ms 256MiB

Minimizing the Beauty of an Array

Minimizing the Beauty of an Array

Alice has an array of n integers and is allowed to perform at most k operations. In one operation, she can increment any element of the array by 1. The beauty of the array is defined as the difference between its maximum and minimum elements. Alice wants to minimize the beauty of the array by applying these operations optimally.

More formally, you are given an array a of n integers and an integer k. You can perform at most k operations, where in one operation you can increase any one element by 1. Your task is to compute the minimum possible value of (max(a) - min(a)) after performing these operations.

Note: It is always optimal to increment the smaller elements. The problem can be solved using a greedy approach after sorting the array.

The following examples illustrate the problem:

  • Example 1: n = 5, k = 8, a = [1, 3, 6, 2, 5] produces a result of 2.
  • Example 2: n = 3, k = 5, a = [4, 7, 4] produces a result of 1.

inputFormat

The first line contains two integers n (the number of elements) and k (the maximum number of operations allowed).

The second line contains n integers representing the elements of the array.

Input is given from standard input (stdin).

outputFormat

Output a single integer representing the minimum possible beauty (i.e., max - min after operations) of the array.

Output should be written to standard output (stdout).

## sample
5 8
1 3 6 2 5
2

</p>