Suppose you have a sorted range (x to y) of values in an array.
x = 3; y = 11; array == 3, 4, 5, 6, 7, 8, 9, 10, 11
But it is possible that some values are duplicated and some are missing, so you might have:
array == 4, 5, 5, 5, 7, 8, 9, 10, 10
What’s the best way in your language to find all duplicates and missing values so you get:
resultMissingValuesArray == 3, 6, 11 resultDuplicatesArray == 5, 5, 10
Here’s some C++ code to get you started:
#include <vector> #include <iostream> #include <algorithm> using namespace std; const int kLastNumber = 50000; // last number expected in array const int kFirstNumber = 3; // first number expected in array int main() { vector<int> myVector; // fill up vector, skip values at the beginning and end to check edge cases for(int x = kFirstNumber + 5; x < kLastNumber - 5; x++) { if(x % 12 != 0 && x % 13 != 0 && x % 17 != 0) myVector.push_back(x); // skip some values else if(x % 9 == 0) { myVector.push_back(x); // add duplicates myVector.push_back(x); } else if(x % 16 == 0) { myVector.push_back(x); // add multiple duplicates myVector.push_back(x); myVector.push_back(x); myVector.push_back(x); } } // put the results in here vector<int> missingValues; vector<int> duplicates; // YOUR CODE GOES HERE // validate missingValues for false positives for(int x = 0; x < (int) missingValues.size(); ++x) { if(binary_search(myVector.begin(), myVector.end(), missingValues.at(x))) cout << 'Oh noes! You missed an unmissed value. Something went horribly, horribly wrong.'; } // validate duplicates (I think... errr) vector<int>::iterator vecItr = myVector.begin(); vector<int>::iterator dupItr = duplicates.begin(); while(dupItr < duplicates.end()) { vecItr = adjacent_find(vecItr, myVector.end()); if(*vecItr != *dupItr) cout << 'Oh noes! Something went horribly, horribly wrong.'; // oh god while(++dupItr != duplicates.end() && *(--dupItr) == *(++dupItr) && *vecItr == *(++vecItr)); ++vecItr; } return 0; }
I didn’t test the validation parts much, so there may be be something wrong with them (especially with the duplicates one).
I will post my own solution as an answer.
Since you’ve marked it language-agnostic, here’s the algorithm I’d use.