Hello Filipe.

Alexander Molchevskyi
2 min readSep 7, 2020

Thanks for your message. You are right this is a wrong solution but it passed all tests in test system of Codility. It seem their tests are incorrect or something like this. I sent them a bug report today. Lets see what they answer. Then I’m going to use this solution. It looks not so beautiful and need for two passes but it works for all sequences even which starts not from 1.

#include <algorithm>
int solution(vector<int> &v) {
int v_size = v.size();
auto min_max = std::minmax_element(v.begin(), v.end());

int min_element = *min_max.first;

if(min_element != 1) {
return false;
}

int max_element = *min_max.second;

// if this is a sequence then max - min + 1 elements must be equal to number of given elements
if ( (max_element - min_element + 1) != v_size) {
return false;
}

// pass through the whole array
for (int i = 0; i < v_size; ++i) {
int j; // auxiliary index

if (v[i] < 0) {
j = -v[i] - min_element;
}
else {
j = v[i] - min_element;
}

if (v[j] > 0) {
v[j] = -v[j];
}
else {
// if the value at index j is negative then it is repeated
return false;
}
}
// If we didn't meet a negative element then this is a sequence
return true;
}

About your sentence “ The problem indicates that each member of the array can be a number in the range [1..1,000,000,000].”

Yes it can but if it is a sequence which must start from 1 by task description it means that it’s members can’t be greater than 100,000

--

--

No responses yet