Nakalchi ReportsAdmin

← Back to analysis

var_a vs var_e

FLAGGED

Problem: two-sum

A→B48%B→A37%Shared fingerprints20
49tokens in the longest matched region
var_a
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int cnt; long long goal;
    cin >> cnt >> goal;
    unordered_map<long long, int> encountered;
    encountered.reserve(cnt * 2);
    for (int cursor = 1; cursor <= cnt; cursor++) {
        long long val;
        cin >> val;
        auto found = encountered.find(goal - val);
        if (found != encountered.end()) {
            cout << found->second << ' ' << cursor << '\n';
            return 0;
        }
        encountered[val] = cursor;
    }
    return 0;
}
var_e
#include <bits/stdc++.h>
using namespace std;

// two-sum, hash map approach, O(n) expected

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int total;          // element count
    long long goal;     // required sum
    cin >> total >> goal;

    unordered_map<long long, int> prior;    // value -> earliest index
    prior.reserve(2 * total);

    int pos = 1;
    while (pos <= total) {
        long long cur;
        cin >> cur;                          // next value
        long long complement = goal - cur;
        auto hit = prior.find(complement);
        if (prior.end() != hit) {
            // found the partner we stored earlier
            cout << hit->second << ' ' << pos << '\n';
            return 0;
        }
        prior[cur] = pos;                    // stash for later
        pos = pos + 1;
    }
    return 0;
}