-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem no. 645.java
39 lines (32 loc) · 996 Bytes
/
Problem no. 645.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
// Ques : Set Mis-match
// Ques Link : https://leetcode.com/problems/set-mismatch/
package com.nitin;
import java.util.Arrays;
public class SetMisMatch {
public static void main(String[] args) {
int[] arr = {2, 1, 4, 2, 6, 5};
System.out.println(Arrays.toString(findErrorNums(arr)));
}
public static int[] findErrorNums(int[] arr) {
int i = 0;
while (i < arr.length) {
int correct = arr[i] - 1;
if (arr[correct] != arr[i]) {
swap(arr, i, correct);
} else {
i++;
}
}
for (int index = 0; index < arr.length; index++) {
if (arr[index] != index + 1) {
return new int[] {arr[index], index + 1};
}
}
return new int[] {-1, -1};
}
public static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}