-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFind_Missing_And_Repeating.java
57 lines (49 loc) · 1.38 KB
/
Find_Missing_And_Repeating.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// { Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
// Driver code
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
String[] str = br.readLine().split(" ");
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(str[i]);
}
int[] ans = new Solve().findTwoElement(a, n);
System.out.println(ans[0] + " " + ans[1]);
}
}
}
class Solve {
int[] findTwoElement(int arr[], int n) {
int missing = -1, repeating = -1;
for (int i = 0; i < n; i++) {
if (arr[Math.abs(arr[i]) - 1] < 0) {
repeating = Math.abs(arr[i]);
} else {
arr[Math.abs(arr[i]) - 1] = -arr[Math.abs(arr[i]) - 1];
}
}
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
missing = i + 1;
}
}
return new int[] { repeating, missing };
// int[] freq = new int[n+1];
// for(int num : arr){
// freq[num]++;
// }
// int missing = -1, repeating = -1;
// for(int i = 1; i<=n; i++){
// if(freq[i] == 0) missing = i;
// if( freq[i] == 2 ) repeating = i;
// }
// return new int[]{repeating, missing};
}
}