c++iteratoriteration

TestDome - Malware Analysis in C++


I'm practicing this problem on TestDome, and I found myself stuck with the final step of the malware process, namely, replacing the correct position with a value of 0. My program accurately prints out the values until we come across the last positions, where instead of printing out the next to last values as such 3, 3, 0, we get 0, 0, 0.

Here is my code:

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

// Update: The problematic is that we need to find a way to not eliminate the first two 77 values.

std::vector<int> simulate(const std::vector<int>& entries) {
    std::vector<int> tempEntries{};
    std::vector<int> simEntries{ entries };

    for (std::vector<int>::const_iterator itX = entries.begin();
        itX != entries.end(); itX++) {
        std::vector<int>::const_iterator itRightT{}; // Iterator of right T position. 
        itRightT = itX + 4;
        if (itRightT != entries.end()) {
            if (*itX <= *itRightT) {
                tempEntries.push_back(*itX);
            }
        }
        else {
            break;
        }
    }

    for (std::vector<int>::const_reverse_iterator itReverseX = entries.rbegin();
        itReverseX != entries.rend(); itReverseX++) {
        std::vector<int>::const_reverse_iterator itLeftT{}; // Iterator of left T position.
        itLeftT = itReverseX + 3;
        if (itLeftT != entries.rend()) {
            if (*itReverseX <= *itLeftT) {
                tempEntries.push_back(*itReverseX);
            }
        }
        else {
            break;
        }
    }

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

    for (std::vector<int>::iterator it = simEntries.begin();
        it != simEntries.end(); it++) {
        if (std::find(tempEntries.begin(), tempEntries.end(), *it) != tempEntries.end()) {
            *it = 0;
        }
    }

    // Desired Result: 1, 0, 0, 5, 0, 0, 0, 3, 3, 0
    //  Actual Result: 1, 0, 0, 5, 0, 0, 0, 0, 0, 0
    return simEntries; 
}

#ifndef RunTests
int main()
{
    std::vector<int> result = simulate({ 1, 2, 0, 5, 0, 2, 4, 3, 3, 3 });
    for (int value : result)
    {
        std::cout << value << " ";
    }
    // Expected output
    // 1, 0, 0, 5, 0, 0, 0, 3, 3, 0
}
#endif

I tried to search for STL functions capable of accomplishing the task above. However, I wasn't able to find any one that was particularly amazing. For instance, std::set_intersection accomplishes the comparison very well; however, I can't modify the elements directly. (Perhaps there is a way to work around this limitation, but I do not know it.)

Any help in solving this problem is appreciated.


Solution

  • Credits to @bijan goshayeshi for the helpful suggestion! Thanks to their input, this revised C++ program successfully passed all 4 test cases.

    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <algorithm>
    
    std::vector<int> simulate(const std::vector<int>& entries) {
        if (entries.size() < 4)
            return entries;
    
        std::vector<int> simEntries{ entries };
        std::vector<std::vector<int>::iterator> tempEntries{};
    
        for (auto itX = std::begin(simEntries); itX != std::end(simEntries) - 4; ++itX) {
           const auto rightT = itX + 4;
           if (*itX <= *rightT)
               tempEntries.push_back(itX);
        }
    
        for (auto itX = std::begin(simEntries) + 3; itX != std::end(simEntries); ++itX) {
            const auto leftT = itX - 3;
            if (*itX <= *leftT)
                tempEntries.push_back(itX);
        }
    
        for (auto it = std::begin(simEntries); it != std::end(simEntries); ++it) {
            if (std::find(std::begin(tempEntries), std::end(tempEntries), it) != std::end(tempEntries))
                *it = 0;
        }
    
        return simEntries;
    }
    
    #ifndef RunTests
    int main() {
        std::vector<int> result = simulate({ 1, 2, 0, 5, 0, 2, 4, 3, 3, 3 });
        for (int value : result) {
            std::cout << value << " ";
        }
        // Expected output
        // 1, 0, 0, 5, 0, 0, 0, 3, 3, 0
    }
    #endif