-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathDisappearedNumber.java
41 lines (37 loc) · 1.15 KB
/
DisappearedNumber.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
//Problem = for finding the disappeared numbers in an array. whose elements are {1,n}
import java.util.ArrayList;
import java.util.Arrays;
public class disapperedNumbers {
public static void main(String[] args) {
int[] arr = {4,3,2,7,8,2,3,1};
cyclicSort(arr);
System.out.println(Arrays.toString(arr));
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if(arr[i] != i+1){
nums.add(i+1);
}
}
System.out.println(nums);
ArrayList<Integer> ans = new ArrayList<>();
if(ans.size() == 0){
System.out.println("I am fool.");
}
}
static void cyclicSort(int[] arr){
int i = 0;
while(i < arr.length){
//arr[arr[i] - 1] = arr[correct index]
if(arr[i] != arr[arr[i] - 1] && arr[i] <= arr.length){
swapArray(arr,i,arr[i] - 1);
}else {
i++;
}
}
}
static void swapArray(int[] arr,int first,int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}