3

To solve 519B I created three vectors that each hold different numbers and occurrences of those numbers for each line. These vectors are also sorted.

eg if the line was 1 1 3 2, the vector would store <1, 2>, <2, 1>, <3, 1>

From there, I would compare one pair from one vector with the pair from the next vector to find the missing number.

This passes most test cases until there are a large number of numbers in each line. I actually saw the answer for this question, but it uses a different method to find the answer. However, I would like to know if there is a way to solve the question with "my way." I read online that using maps and other containers could yield a faster result, but I'm skeptical that using another container would really make that big of a difference. Any thoughts?

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

int has(std::vector<std::pair<int, int>> &error, int i) {
    for (int j = 0; j < error.size(); j++) {
        if (error[j].first == i) {
            return j;
        }
    }

    return -1;
}

std::vector<std::pair<int, int>> generate(int n) {
    int temp;
    std::vector<std::pair<int, int>> tempVec;

    for (int i = 0; i < n; i++) {
        std::cin >> temp;
        int pos = has(tempVec, temp);
        if (pos != -1) {
            tempVec[pos].second++;
        } else {
            tempVec.push_back(std::make_pair(temp, 1));
        }
    }

    return tempVec;
}

bool sortPair(const std::pair<int, int> &a, const std::pair<int, int> &b) {
    return a.first < b.first;
}

int main() {
    int n, temp;
    std::cin >> n;

    std::vector<std::pair<int, int>> a = generate(n), b = generate(n - 1), c = generate(n - 2);
    std::sort(a.begin(), a.end(), sortPair);
    std::sort(b.begin(), b.end(), sortPair);
    std::sort(c.begin(), c.end(), sortPair);

    bool found = false;

    for (int i = 0; i < n - 1; i++) {
        if (a[i] != b[i]) {
            found = true;
            std::cout << a[i].first << std::endl;
            break;
        }
    }

    if (!found) {
        std::cout << a[n - 1].first << std::endl;
    }

    bool found2 = false;

    for (int i = 0; i < n - 2; i++) {
        if (b[i] != c[i]) {
            found2 = true;
            std::cout << b[i].first << std::endl;
            break;
        }
    }

    if (!found2) {
        std::cout << b[n - 2].first << std::endl;
    }
}
  • 2
    Your `generate()` function is too slow. In it, you perform a linear search through `tempVec` for each of the `N` integers you input. Since linear search takes `O(container size)` time, here it takes `O(tempVec.size()) = O(i)` time in the worst case (`i` being the loop variable). Thus, across the whole loop, `generate()` takes `O(1 + 2 + ... + N) = O(N^2)` in the worst case, and since `N` can be up to `1e5`, this is much too slow. The other parts of your code take `O(N)` in total, meaning that it's just your input that's making your program time out. – Telescope Sep 17 '22 at 17:43
  • In my has() function, should I use binary search then? I can't really think of a better way to check if tempVec has those elements or not. – Benjamin Chen Sep 17 '22 at 17:47
  • 1
    Binary search seems like a good idea, but it wouldn't actually work here. Why? Say that `tempVec` is sorted, and you binary search through it, only to find that the entry you want doesn't exist. In this case, you'd need to add a new `pair ` to `tempVec` - but you'd also need to make sure it's added at the correct index so that `tempVec` remains sorted. This is the problem; inserting an element into a `vector` at an index `i` takes `O(vector.size() - i)` time, which is `O(N)` in the worst case. So even if you use binary search, the fact that you use `vector`s undermines any speedup. – Telescope Sep 17 '22 at 17:49
  • 1
    This, in fact, is probably the biggest limitation of a `vector`; if you want to keep it sorted and also insert an element, you'd need `O(N)` time. On the other hand, a `std::set` can insert elements in `O(logN)` time, and a `std::unordered_set` can insert elements in `O(1)` time (most of the time). I'm guessing you aren't very familiar with exactly what those containers do (and their purposes), so go and learn that. A **great resource** to do that is here: https://cses.fi/book/book.pdf. Go to page 45 and read the short chapter on Data Structures. – Telescope Sep 17 '22 at 17:53
  • Thank you, I was just about to ask you about whether or not I should use set. – Benjamin Chen Sep 17 '22 at 17:55

0 Answers0