Nakalchi ReportsAdmin

← Back to analysis

sol01 vs sol10

Problem: two-sum

A→B30%B→A17%Shared fingerprints12
29tokens in the longest matched region
sol01
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n; long long target;
    cin >> n >> target;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] + a[j] == target) {
                cout << i + 1 << " " << j + 1 << "\n";
                return 0;
            }
        }
    }
    return 0;
}
sol10
#include <iostream>
#include <set>
#include <vector>
using namespace std;

int main() {
    int n; long long target;
    cin >> n >> target;
    vector<long long> arr(n);
    multiset<long long> pool;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        pool.insert(arr[i]);
    }
    for (int i = 0; i < n; i++) {
        // remove current element so we don't pair it with itself
        pool.erase(pool.find(arr[i]));
        if (pool.count(target - arr[i])) {
            // second index = first later occurrence of the complement
            for (int j = i + 1; j < n; j++) {
                if (arr[j] == target - arr[i]) {
                    cout << i + 1 << " " << j + 1 << "\n";
                    return 0;
                }
            }
        }
    }
    return 0;
}