-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-2.java
55 lines (34 loc) · 1.1 KB
/
Problem-2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @author AkashGoyal
* @date 26/05/2021
*/
/**
--------------------- Problem----------->> Minimize the Heights II
Problem Link :- https://practice.geeksforgeeks.org/problems/minimize-the-heights3351/1
Reference:- https://www.youtube.com/watch?v=Av7vSnPSCtw
*/
class Solution {
int getMinDiff(int[] arr, int n, int k) {
// code here
// code here
//sort the array so tat we can work on adjacent elements
Arrays.sort(arr);
//when k>n then this should be the result;(we increase everything by k then take out the difference)
int result= arr[n-1]-arr[0];
//by default this should be the minimum((longest-k)-(smallest+k))
int largest=arr[n-1]-k;
int smallest=arr[0]+k;
for(int i=0;i<n-1;i++)
{
int min=Math.min(smallest,arr[i+1]-k);
int max=Math.max(largest,arr[i]+k);
if(min<0)
{
//skip the case
continue;
}
result=Math.min(result,max-min);
}
return result;
}
}