Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 558 Bytes

21.md

File metadata and controls

26 lines (18 loc) · 558 Bytes
 int getMinDiff(int arr[], int n, int k) {
        
        sort(arr,arr+n);
        
        int ans=arr[n-1]-arr[0];
        
        int s=arr[0]+k;
        int l=arr[n-1]-k;
    
        int mi,ma;
        
       for(int i=0;i<n-1;i++)
       {
           mi=min(s,arr[i+1]-k);
           ma=max(l,arr[i]+k);
           
           if(mi<0) continue;
                ans=min(ans,ma-mi);
       }
        
        return ans;
    }