-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMissig.java
More file actions
38 lines (33 loc) · 1.14 KB
/
Copy pathFindMissig.java
File metadata and controls
38 lines (33 loc) · 1.14 KB
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
import java.util.ArrayList;
import java.util.List;
public class FindMissig {
public static void main(String[] args) {
int arr[] = {8,8,3,5,5,5,6,1};
List<Integer> missingNumbers = findDisappearedNumbers(arr);
System.out.println("Disappeared numbers are: " + missingNumbers);
}
public static List<Integer> findDisappearedNumbers(int[] arr) {
int i = 0;
while (i < arr.length) {
int correct = arr[i] - 1;
if (arr[i] != arr[correct]) {
swap(arr, i, correct);
} else {
i++;
}
}
// Find all indices where the numbers are not in the correct place
List<Integer> missingNumbers = new ArrayList<>();
for (int index = 0; index < arr.length; index++) {
if (arr[index] != index + 1) {
missingNumbers.add(index + 1);
}
}
return missingNumbers;
}
public static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}