MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / getMinDiff

Method getMinDiff

MinimizeTheHeights.java:3–22  ·  view source on GitHub ↗
(int[] arr, int n, int k)

Source from the content-addressed store, hash-verified

1class Solution
2{
3 int getMinDiff(int[] arr, int n, int k)
4 {
5 int min=0,max=0,r=0;
6 Arrays.sort(arr);
7 r=arr[n-1]-arr[0];
8 for(int i=1;i<n;i++)
9 {
10 if(arr[i]>=k)
11 {
12 max=Math.max(arr[i-1]+k,arr[n-1]-k);
13 min=Math.min(arr[i]-k,arr[0]+k);
14 r=Math.min(r,max-min);
15 }
16 else
17 {
18 continue;
19 }
20 }
21 return r;
22 }
23}

Callers

nothing calls this directly

Calls 1

sortMethod · 0.80

Tested by

no test coverage detected