1
class Solution {
2
public int minimumDeletions(int[] nums) {
3
int max = Integer.MIN_VALUE;
4
int min = Integer.MAX_VALUE;
5
int minInd = 0;
6
int maxInd = 0;
7
int n = nums.length;
8

9
// First Find out the max and min element index
10
for (int i = 0; i < n; i++) {
11
if (nums[i] > max) {
12
max = nums[i];
13
maxInd = i;
14
}
15

16
if (nums[i] < min) {
17
min = nums[i];
18
minInd = i;
19
}
20
}
21

22
// if both index are same then return the part in which less number of elements are there
23
if (maxInd == minInd) {
24
return Math.min(maxInd + 1, n - maxInd);
25
}
26

27
// max element is right side of min element
28
if (maxInd > minInd) {
29
int count =
30
Math.min(
31
maxInd + 1,
32
n - minInd); // min of all the elements till max element and all the elements to the
33
// right of min element
34
int count1 = minInd + 1 + (n - maxInd); // all elements to the left of min and right of max
35
return Math.min(count, count1); // min of both
36
}
37
// min element is right side of the max element
38
else {
39
int count = Math.min(minInd + 1, n - maxInd);
40
int count1 = maxInd + 1 + (n - minInd);
41
return Math.min(count, count1);
42
}
43
}
44
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0