forked from Anuragcoderealy/Codenewhacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMismatch.java
More file actions
25 lines (25 loc) · 719 Bytes
/
SetMismatch.java
File metadata and controls
25 lines (25 loc) · 719 Bytes
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
public class SetMismatch {
public int[] findErrorNums(int[] nums) {
int i = 0;
while(i< nums.length){
int correct = nums[i]-1;
if(nums[i] != nums[correct]){
swap(nums, i,correct);
}else {
i++;
}
}
// search for first missing number
for (int index = 0;index< nums.length;index++){
if (nums[index] != index + 1){
return new int[] {nums[index], index+1};
}
}
return new int[] {};
}
static void swap(int[] arr, int first, int second){
int temp= arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}