I'm not sure I got your question. How do you know right element to remove (bad ones)?
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
For example, 12123 will get transformed to 1223 instead of 123, 12342169 will get transformed to 1234169.. it wil replace 2 with 1 and jump to 6 to check if its greater than 1, but it wont check if 1 is greater than 4. if you know what I mean.
you mean sorting?
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
Sort the values
Copy only the first of any duplicates to a new array.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
you could tune the codes to make it become safer, faster and generic
as your need, if there are bugs, please tell me, I will try my best to
fix it
size_t *remove_adjacent(size_t *first, size_t *last, std::vector<size_t> &result)
{
if(first == last) return 0;
size_t *next = first + 1;
if(next == last) result.push_back(*first);
while(next != last)
{
if(*first <= *next)
{
std::cout<<*first<<", "<<*next<<std::endl;
result.push_back(*first);
++first; ++next;
if(next == last)
result.push_back(*first);
}
else if(*first > *next)
{
std::cout<<*first<<", "<<*next<<std::endl;
result.push_back(*first);
if(last - next > 1)
{
first += 2;
next += 2;
}
else
{
return last;
}
}
}
}
void case_03()
{
size_t A[] = { 1, 2, 3, 4, 2, 1, 6, 9 };
std::vector<size_t> result;
remove_adjacent(A, A + sizeof(A) / sizeof(*A), result);
std::copy(result.begin(), result.end(), std::ostream_iterator<size_t>(std::cout, "\n") );
}
This is bad!!!!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392