Geeksforgeeks

It's live! The biggest programming festival has begun! Check out Geek Week now!

Error

Given an array **arr[]** denoting heights of **N** towers and a positive integer **K**, you have to modify the height of each tower either by increasing or decreasing them by **K** only once.

Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower.

**Note: **Assume that height of the tower can be negative.

A slight modification of the problem can be found here.

**Example 1:**

**Input:
**K = 2, N = 4
Arr[] = {1, 5, 8, 10}
**Output:**
5
**Explanation:**
The array can be modified as
{3, 3, 6, 8}. The difference between
the largest and the smallest is 8-3 = 5.

**Example 2:**

**Input:
**K = 3, N = 5
Arr[] = {3, 9, 12, 16, 20}
**Output:**
11
**Explanation:**
The array can be modified as
{6, 12, 9, 13, 17}. The difference between
the largest and the smallest is 17-6 = 11.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **getMinDiff()** which takes the **arr[], n** and **k **as input parameters and returns an integer denoting the minimum difference.

**Expected Time Complexity:** O(N*logN)

**Expected Auxiliary Space:** O(N)

**Constraints**

1 ≤ K ≤ 10^{4}

1 ≤ N ≤ 10^{5}

1 ≤ Arr[i] ≤ 10^{5}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Minimize the Heights I

...