Skip to content

Commit 5695ae7

Browse files
authored
Create Minimum Number of Operations to Make Array Continuous.java
1 parent fd343f2 commit 5695ae7

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int minOperations(int[] nums) {
3+
Set<Integer> set = new HashSet<>();
4+
for (int num : nums) {
5+
set.add(num);
6+
}
7+
int[] arr = new int[set.size()];
8+
int idx = 0;
9+
for (Integer num : set) {
10+
arr[idx++] = num;
11+
}
12+
Arrays.sort(arr);
13+
int n = nums.length;
14+
int result = n;
15+
int j = 0;
16+
for (int i = 0; i < arr.length; i++) {
17+
while (j < arr.length && arr[j] < arr[i] + n) {
18+
j++;
19+
}
20+
int count = j - i;
21+
result = Math.min(result, n - count);
22+
}
23+
return result;
24+
}
25+
}

0 commit comments

Comments
 (0)