Minimize the Heights I | Practice | GeeksforGeeks

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 ≤ 104
1 ≤ N ≤ 105
1 ≤ Arr[i] ≤ 105
0
viktor1 day ago
All testcase passed : JAVA Solution
int getMinDiff(int[] arr, int n, int k) {       // code here       if(n==1){           return 0;       }       Arrays.sort(arr);       int diff = arr[n-1] - arr[0];       int minimum,maximum;       for(int i=1;i<n;i++){           maximum = Math.max(arr[i-1]+k, arr[n-1]-k);           minimum = Math.min(arr[0]+k, arr[i]-k);           diff = Math.min(diff,maximum-minimum);       }       return diff;   }
0
prasantpoudel332 weeks ago
arr.sort()
        for i in range(n):
            if arr[i]>k:
                arr[i]=arr[i]-k
            else:
                arr[i]=arr[i]+k
        arr.sort()
        return arr[n-1]-arr[0]
 
for one test case
arr=[2,6,3,4,7,2,10,3,2,1]
k=5
n=10
the output should be 8 but while submiting the code this test case show the desire output is 7
but according to soln it should be 8
0
swapniltayal4223 weeks ago
class Solution{   public:   int getMinDiff(int arr[], int n, int k) {       // code here       if (n == 0){           return -1;       }              sort(arr, arr+n);       int diff = (arr[n-1] - arr[0]);       int maxi, mini;              for (int i=1; i<n; i++){                      maxi = max(arr[i-1]+k, arr[n-1]-k);           mini = min(arr[0] + k, arr[i]-k);           diff = min(maxi - mini, diff);                  }return diff;   }};
0
georgemonier19821 month ago
it was a hard one I have to say :)
 
====================
from queue import PriorityQueue
class Solution:   def getMinDiff(self, arr, n, k):       # code here       if n == 1 :           return 0
       q = PriorityQueue()       remove_dub = set()       for x in arr :           remove_dub.add(x)       for h in remove_dub :           q.put(h)
       sorted_h = []
       for _ in range(len(remove_dub)):           sorted_h.append(q.get())
 
       Tallest  = None       Shortest = None       Diff = None
       if sorted_h[-1] - k >= sorted_h[-2] + k :           Diff = (sorted_h[-1] - k) - (sorted_h[0] + k)           return Diff
       if sorted_h[0] + k <= sorted_h[1] - k :           Diff = (sorted_h[-1] - k) - (sorted_h[0] + k)           return Diff
       for i in range(2,len(sorted_h)+1):
           if i == len(sorted_h) :               if Diff :                   Diff = min((sorted_h[-1] - k - sorted_h[0] + k) , Diff )               else :                   Diff = (sorted_h[-1] - k - sorted_h[0] + k)
           if sorted_h[-i] + k >= sorted_h[-i+1] - k :                   current_plus_k  = sorted_h[-i] + k                   tallest = max(current_plus_k,sorted_h[-1]-k)                   previ_minus_k   = sorted_h[-i+1] - k                   smallest_plus_k = sorted_h[0] + k                   shortest = min (smallest_plus_k,previ_minus_k)                   if not Diff :                       Diff = tallest - shortest                   else :                       Diff = min (tallest - shortest , Diff)                   #print(tallest,shortest,Diff)           else :               Diff = (sorted_h[-1] - k) - (sorted_h[0] + k)               return Diff
 
       return Diff
0
ghoshghoshbishal1 month ago
class Solution {   int getMinDiff(int[] arr, int n, int k) {       Arrays.sort(arr);              int diff = arr[n-1] - arr[0];       int maxNow = arr[n-1] - k;       int minNow = arr[0] + k;       int max = maxNow;       int min = minNow;              for(int i = 0; i < n - 1; i++) {           max = Math.max(maxNow, arr[i] + k);           min = Math.min(minNow, arr[i+1] - k);           diff = Math.min(diff, max - min);       }       return diff;   }} 
0
subhashishde081 month ago
class Solution{   
public:
    int getMinDiff(int arr[], int n, int k) {
        // code here
        if(n==1){
            return 0;
        }
        sort(arr,arr+n);
        int diff = arr[n-1] - arr[0];
        int mini,maxi;
        for(int i=1;i<n;i++){
        
            maxi = max(arr[i-1]+k, arr[n-1]-k);
            mini = min(arr[0]+k, arr[i]-k);
            diff = min(diff,maxi-mini);
        }
        return diff;
    }
};
+5
parthtagalpallewar1232 months ago
Can Anyone Help me by answering this question?
K = 5
int[] arr = {2 6 3 4 7 2 10 3 2 1}
 
Why the answer to this question is 7?
0
naveenmnka15
This comment was deleted.
+3
0231216rahulpatel2 months ago
sort(arr,arr+n);       int ans=arr[n-1]-arr[0];       int mi,ma;       for(int i=1;i<n;i++)       {           mi=min(arr[0]+k,arr[i]-k);           ma=max(arr[i-1]+k,arr[n-1]-k);           ans=min(ans,ma-mi);       }       return ans;
0
sunghunet3 months ago
int getMinDiff(int arr[], int n, int k) {
        
    sort(arr, arr+n);
    int minimum_range = (*(arr+(n-1)) - *(arr)), short_range=0, long_range=0;

    for( int i = 0; i < n ; i++ ) {
        short_range = ((arr[0]+k) > (arr[i+1]-k)) ? arr[i+1]-k : arr[0]+k;
        long_range = ((arr[i]+k > arr[n-1]-k)) ? arr[i]+k : arr[n-1]-k;
        minimum_range = (minimum_range > (long_range-short_range)) ? long_range-short_range : minimum_range;
    }

    return minimum_range;
} 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.