[InterviewBit] Repeat and Missing Number Array

Repeat and Missing Number Array

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
vector<int> vis;
vector<int> Solution::repeatedNumber(const vector<int> &A) {
int n = A.size();
vis = vector<int>(n + 1);

int dup = -1;
for(auto& a : A) if(++vis[a] == 2) dup = a;
for(int i = 1; i <= n; i++) if(!vis[i]) return {dup, i};
return {dup, -1};
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/16/PS/interviewbit/repeat-and-missing-number-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.