fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef pair<vector<int>, vector<int>> pvivi;
  6.  
  7. pvivi findMissingAndDup(int a[], int len) {
  8. vector<int> missing, dup;
  9. for (int i = 0; i < len; i++) {
  10. while (a[a[i]] != a[i])
  11. swap(a[i], a[a[i]]);
  12. }
  13.  
  14. for (int i = 0; i < len; i++) {
  15. if (a[i] != i) {
  16. missing.push_back(i);
  17. dup.push_back(a[i]);
  18. }
  19. }
  20. return make_pair(missing, dup);
  21. }
  22.  
  23. int main() {
  24. int a[] = {0, 5, 3, 3, 4, 5};
  25. pvivi p = findMissingAndDup(a, sizeof(a)/sizeof(a[0]));
  26. vector<int> vm = p.first;
  27. vector<int> vd = p.second;
  28. cout << "Missing: ";
  29. for (int i:vm) cout<<i<<" ";
  30. cout << "\nDup: ";
  31. for (int i:vd) cout<<i<<" ";
  32. // your code goes here
  33. return 0;
  34. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
Missing: 1 2 
Dup: 5 3