-2

I'm trying to find the difference of these 2 sets {1,2,3,4,5,6} and {1, 3, 6, 4, 1, 2} . The missing vector should contain just {5} but it contains {2,4,5} . What am I doing wrong here ?

#include <set>
#include <vector>
#include <iostream>
using namespace std;
int solution(vector<int> &A) {
    vector<int> missing;
    vector<int> range(A.size());
    for(int i=1; i<= A.size(); i++) range[i-1] = i;

    set_difference(range.begin(), range.end(), A.begin(), A.end(), std::inserter(missing, missing.end()));
    return missing[0]; // Missing contains 2,4,5 where as it should had contain just 5
}

int main(){
    vector<int> A = {1, 3, 6, 4, 1, 2};
    cout << solution(A);
   return 0;
}
sapy
  • 8,952
  • 7
  • 49
  • 60

1 Answers1

0

The 2nd argument to std::inserter method should be missing.begin() not missing.end().

std::sort(A.begin(),A.end());

set_difference(range.begin(), range.end(), A.begin(), A.end(), std::inserter(missing, missing.begin()));
user3863360
  • 200
  • 7