Minimise the maximum difference between the heights

Given an array of integers A representing heights of towers and a positive integer B.
It is allowed to either increase or decrease the height of every tower by B (only once).

Find and return the minimum possible difference between the heights of the longest
and the shortest tower after modifications.

Note: It is mandatory to either increase or decrease the height of every tower.



Input Format

The first argument given is the integer array A.
The second argument given is the integer B.

Output Format

Return the minimum possible difference.

Constraints

2 <= length of the array <= 100000
-10^9 <= A[i] <= 10^9 
1 <= B <= 10^9

For Example

Input 1:
    A = [1, 2, 3, 4, 5]
    B = 2
Output 1:
    3

Input 2:
    A = [5, 17, 100, 11]
    B = 10
Output 2:
    75
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Minimise the maximum difference between the heights on Interview Code Editor
Sign Up
to access hints and editorial solutions for Minimise the maximum difference between the heights

Discussion


Loading...
Click here to start solving coding interview questions