-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfind missing and repeating
61 lines (52 loc) · 1.4 KB
/
find missing and repeating
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
/*
Given an unsorted array of size N of positive integers. One number 'A' from set {1, 2, …N} is missing and one number 'B' occurs twice in
array. Find these two numbers.
Note: If you find multiple answers then print the Smallest number found. Also, expected solution is O(n) time and constant extra space.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line
of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ...,
AN denoting the elements of the array.
Output:
Print B, the repeating number followed by A which is missing in a single line.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 106
1 ≤ A[i] ≤ N
Example:
Input:
2
2
2 2
3
1 3 3
Output:
2 1
3 2
Explanation:
Testcase 1: Repeating number is 2 and smallest positive missing number is 1.
Testcase 2: Repeating number is 3 and smallest positive missing number is 2.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t;
while(t--){
int n,rep,miss; cin>>n;
int h[n+1]={0};
for(int i=0; i<n; i++){
int a;
cin>>a;
h[a]++;
}
for(int i=1; i<=n; i++){
if(h[i]==2) {
rep=i;
}
if(h[i]==0){
miss=i;
}
}
cout<<rep<<" "<<miss<<endl;
}
return 0;
}