-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind the repeating and the missing
62 lines (55 loc) · 1.49 KB
/
Find the repeating and the missing
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
58
59
60
61
62
//{ 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]);
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solve {
int[] findTwoElement(int arr[], int n)
{
int[] res = new int[arr.length];
int[] result = new int[2];
int index=0, c=0;
for(int i=0; i<arr.length; i++)
{
index=arr[i];
c=res[index-1];
res[index-1]=++c;
// System.out.println(res[0]+" "+res[1]);
}
for(int i=0; i<res.length; i++)
{
if(res[i]>1)
{
result[0]=i+1;
}
if(res[i]==0)
{
result[1]=i+1;
}
if(result[0]!=0 && result[1]!=0)
{
break;
}
}
return result;
}
}